svelte-declarative-testing 0.3.1 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -45,25 +45,25 @@ Your tests can look like this:
45
45
  </script>
46
46
 
47
47
  <Test it="opens the menu when the trigger is clicked">
48
- <!-- children of the Test are rendered automatically -->
49
- <Popover>
50
- <PopoverTrigger>Open Menu</PopoverTrigger>
51
- <PopoverMenu>
52
- <PopoverMenuItem href="/account">Account</PopoverMenuItem>
53
- <PopoverMenuItem href="/logout">Log out</PopoverMenuItem>
54
- </PopoverMenu>
55
- </Popover>
48
+ <!-- Anything passed via the mount() snippet is rendered for each test -->
49
+ {#snippet mount()}
50
+ <Popover>
51
+ <PopoverTrigger>Open Menu</PopoverTrigger>
52
+ <PopoverMenu>
53
+ <PopoverMenuItem href="/account">Account</PopoverMenuItem>
54
+ <PopoverMenuItem href="/logout">Log out</PopoverMenuItem>
55
+ </PopoverMenu>
56
+ </Popover>
57
+ {/snippet}
56
58
 
57
59
  <!-- one or more test functions can be provided as checks -->
58
- {#snippet checks()}
59
- <Check
60
- fn={async (screen) => {
61
- expect(screen.getByRole('menu', { includeHidden: true })).not.toBeVisible();
62
- await screen.getByRole('button', { name: 'Open Menu' }).click();
63
- expect(screen.getByRole('menu')).toBeVisible();
64
- }}
65
- />
66
- {/snippet}
60
+ <Check
61
+ fn={async (screen) => {
62
+ expect(screen.getByRole('menu', { includeHidden: true })).not.toBeVisible();
63
+ await screen.getByRole('button', { name: 'Open Menu' }).click();
64
+ expect(screen.getByRole('menu')).toBeVisible();
65
+ }}
66
+ />
67
67
  </Test>
68
68
  ```
69
69
 
@@ -173,8 +173,8 @@ rendered for each test.
173
173
  | Properties | Type | Description |
174
174
  | ---------- | ---------- | ------------------------------------------------------------------------------------- |
175
175
  | `label` | `string` | The descriptive label for the test suite |
176
- | `children` | `Snippet` | Any component(s) that you wish to render for the tests |
177
- | `tests` | `Snippet` | A snippet block containing one or more `<Test>` elements |
176
+ | `mount` | `Snippet` | Any component(s) that you wish to render for the tests |
177
+ | `children` | `Snippet` | A snippet block containing one or more `<Test>` elements |
178
178
  | `skip` | `any` | Skips the suite if truthy, just like `describe.skip()` |
179
179
  | `only` | `any` | Only run this suite if truthy, just like `describe.only()` |
180
180
  | `todo` | `any` | Mark the suite as todo if truthy, just like `describe.todo()` |
@@ -184,17 +184,17 @@ rendered for each test.
184
184
 
185
185
  ```svelte
186
186
  <Describe label="My Integration Tests">
187
- <Button>My button</Button>
187
+ {#snippet mount()}
188
+ <Button>My button</Button>
189
+ {/snippet}
188
190
 
189
- {#snippet tests()}
190
- <Test it="renders an accessible button">
191
- <!-- Test body goes here -->
192
- </Test>
191
+ <Test it="renders an accessible button">
192
+ <!-- Test body goes here -->
193
+ </Test>
193
194
 
194
- <Test it="does something when I click the button">
195
- <!-- Test body goes here -->
196
- </Test>
197
- {/snippet}
195
+ <Test it="does something when I click the button">
196
+ <!-- Test body goes here -->
197
+ </Test>
198
198
  </Describe>
199
199
  ```
200
200
 
@@ -213,8 +213,8 @@ parent `<Describe>` are rendered.
213
213
  | Properties | Type | Description |
214
214
  | ---------- | ---------- | -------------------------------------------------------------------------------- |
215
215
  | `it` | `string` | The description of the test |
216
- | `children` | `Snippet` | Any component(s) that you wish to render for the test |
217
- | `checks` | `Snippet` | A snippet block containing one or more `<Check>` elements |
216
+ | `mount` | `Snippet` | Any component(s) that you wish to render for the test |
217
+ | `children` | `Snippet` | A snippet block containing one or more `<Check>` elements |
218
218
  | `skip` | `any` | Skips the test if truthy, just like `test.skip()` |
219
219
  | `only` | `any` | Only run this test if truthy, just like `test.only()` |
220
220
  | `todo` | `any` | Mark the test as todo if truthy, just like `test.todo()` |
@@ -255,7 +255,7 @@ You can provide as many checks as you like and they will run sequentially.
255
255
  {#snippet checks()}
256
256
  <Check fn={({queryByRole}) => {
257
257
  expect(queryByRole("button")).toBeIntheDocument();
258
- }/>
258
+ }}/>
259
259
  {/snippet}
260
260
  </Describe>
261
261
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-declarative-testing",
3
- "version": "0.3.1",
3
+ "version": "0.3.3",
4
4
  "description": "A way to mount your Svelte test components declaratively",
5
5
  "type": "module",
6
6
  "module": "src/index.js",
@@ -37,7 +37,6 @@
37
37
  "dependencies": {
38
38
  "magic-string": "^0.30.21",
39
39
  "source-map": "^0.7.6",
40
- "ts-essentials": "^10.1.1",
41
40
  "zimmerframe": "^1.1.4"
42
41
  },
43
42
  "devDependencies": {
File without changes
@@ -1,26 +1,41 @@
1
1
  import type { Snippet, Component } from 'svelte';
2
- import { XOR } from 'ts-essentials';
3
2
 
4
- export type ModifierProps = XOR<
5
- /* These are all mutually exclusive */
6
- { only: boolean },
7
- { todo: boolean },
8
- { skip: boolean },
9
- { skipIf: () => unknown },
10
- { runIf: () => unknown }
11
- >;
3
+ type KeysAsExclusiveUnion<T> = {
4
+ [K in keyof T]: { [P in K]: T[P] } & { [P in Exclude<keyof T, K>]?: never };
5
+ }[keyof T];
6
+
7
+ export type ModifierProps = {
8
+ /* These are all mutually exclusive on Describe and Test */
9
+ only?: boolean;
10
+ todo?: boolean;
11
+ skip?: boolean;
12
+ skipIf?: () => boolean;
13
+ runIf?: () => boolean;
14
+ fails?: boolean;
15
+ };
16
+
17
+ type ModifiersWithoutFails = Omit<ModifierProps, 'fails'>;
18
+
19
+ type DescribeModifierProps =
20
+ | KeysAsExclusiveUnion<ModifiersWithoutFails>
21
+ | { [K in keyof ModifiersWithoutFails]?: never };
22
+
23
+ type TestModifierProps =
24
+ | KeysAsExclusiveUnion<ModifierProps>
25
+ | { [K in keyof ModifierProps]?: never };
12
26
 
13
27
  export type DescribeProps = {
14
28
  label: string;
15
29
  children: Snippet;
16
30
  mount?: Snippet;
17
- } & ModifierProps;
31
+ fails?: never;
32
+ } & DescribeModifierProps;
18
33
 
19
34
  export type BaseTestProps = {
20
35
  it: string;
21
36
  children: Snippet;
22
37
  mount?: Snippet;
23
- } & XOR<ModifierProps, { fails: boolean }>;
38
+ } & TestModifierProps;
24
39
 
25
40
  type RenderResult = {
26
41
  unmount: () => void | Promise<void>;
@@ -1,6 +1,6 @@
1
1
  // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
2
 
3
- exports[`vitest post plugin > produces code that mounts the component and returns a render result 1`] = `"export default function AComponent() {};import { mount } from "svelte"; mount((await import(import.meta.url)).default, { target: document.body });"`;
3
+ exports[`vitest post plugin > produces code that mounts the component and returns a render result 1`] = `"export default function AComponent() {};import { mount as svelte_declarative_testing_mount } from "svelte";svelte_declarative_testing_mount(AComponent_test, { target: document.body });"`;
4
4
 
5
5
  exports[`vitest pre plugin > produces code with a dummy test suite 1`] = `
6
6
  "
@@ -0,0 +1,3 @@
1
+ import { type Plugin } from 'vitest/config';
2
+
3
+ export default function getPlugins(): Plugin[];
@@ -17,8 +17,8 @@ import { SourceMapGenerator, SourceMapConsumer } from 'source-map';
17
17
  * imported so that the tests run.
18
18
  */
19
19
 
20
- const mountCode =
21
- ';import { mount } from "svelte"; mount((await import(import.meta.url)).default, { target: document.body });';
20
+ const getMountCode = (/**@type {string}*/ name) =>
21
+ `;import { mount as svelte_declarative_testing_mount } from "svelte";svelte_declarative_testing_mount(${name}, { target: document.body });`;
22
22
 
23
23
  const testFileRegex = /\.(?:test|spec)\.svelte$/;
24
24
  const getNameFromAttr = (node, attr) =>
@@ -123,9 +123,10 @@ const post = () => ({
123
123
 
124
124
  const s = new MagicString(code);
125
125
 
126
- // We mount by reimporting the default export of the Svelte file (the
127
- // component). This is the magic that runs the tests.
128
- s.append(mountCode);
126
+ // TODO: find a more robust way to get the component name
127
+ const name = id.split('/').pop().replace('.svelte', '').replace(/\./g, '_');
128
+
129
+ s.append(getMountCode(name));
129
130
 
130
131
  return { code: s.toString() };
131
132
  },
@@ -1,8 +0,0 @@
1
- {
2
- "recommendations": [
3
- "svelte.svelte-vscode",
4
- "vitest.explorer",
5
- "esbenp.prettier-vscode",
6
- "dbaeumer.vscode-eslint"
7
- ]
8
- }