svelte-declarative-testing 0.3.0 → 0.3.2

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/eslint.config.js DELETED
@@ -1,40 +0,0 @@
1
- import prettier from 'eslint-config-prettier';
2
- import { fileURLToPath } from 'node:url';
3
- import { includeIgnoreFile } from '@eslint/compat';
4
- import js from '@eslint/js';
5
- import svelte from 'eslint-plugin-svelte';
6
- import { defineConfig } from 'eslint/config';
7
- import globals from 'globals';
8
- import ts from 'typescript-eslint';
9
-
10
- const gitignorePath = fileURLToPath(new URL('./.gitignore', import.meta.url));
11
-
12
- export default defineConfig(
13
- includeIgnoreFile(gitignorePath),
14
- js.configs.recommended,
15
- ...ts.configs.recommended,
16
- ...svelte.configs.recommended,
17
- prettier,
18
- ...svelte.configs.prettier,
19
- {
20
- languageOptions: { globals: { ...globals.browser, ...globals.node } },
21
-
22
- rules: {
23
- // typescript-eslint strongly recommend that you do not use the no-undef lint rule on TypeScript projects.
24
- // see: https://typescript-eslint.io/troubleshooting/faqs/eslint/#i-get-errors-from-the-no-undef-rule-about-global-variables-not-being-defined-even-though-there-are-no-typescript-errors
25
- 'no-undef': 'off',
26
- },
27
- },
28
- {
29
- files: ['**/*.svelte', '**/*.svelte.ts', '**/*.svelte.js'],
30
-
31
- languageOptions: {
32
- parserOptions: {
33
- projectService: true,
34
- extraFileExtensions: ['.svelte'],
35
- parser: ts.parser,
36
- svelteConfig: {},
37
- },
38
- },
39
- },
40
- );
@@ -1,46 +0,0 @@
1
- <script>
2
- import { fireEvent } from '@testing-library/svelte';
3
- import { Test, Describe, Check } from '../../src/components/testing-library';
4
- </script>
5
-
6
- <Test it="finds the rendered component">
7
- {#snippet mount()}
8
- <button>Click me</button>
9
- {/snippet}
10
-
11
- <Check
12
- fn={({ getByRole }) => {
13
- expect(getByRole('button', { name: 'Click me' })).not.toBe(null);
14
- }}
15
- />
16
- </Test>
17
-
18
- <Describe label="Basic test suite">
19
- {#snippet mount()}
20
- <button>Click me</button>
21
- {/snippet}
22
-
23
- <Describe label="Nested describe">
24
- <Test it="mounts the test's snippet instead of the describe's snippet">
25
- {#snippet mount()}
26
- <button>No, click me</button>
27
- {/snippet}
28
-
29
- <Check
30
- fn={({ queryByRole }) => {
31
- expect(queryByRole('button', { name: 'Click me' })).toBe(null);
32
- expect(queryByRole('button', { name: 'No, click me' })).not.toBe(null);
33
- }}
34
- />
35
- </Test>
36
-
37
- <Test it="mounts the describe's snippet if the test doesn't have its own snippet">
38
- <Check
39
- fn={({ queryByRole }) => {
40
- expect(queryByRole('button', { name: 'Click me' })).not.toBe(null);
41
- expect(queryByRole('button', { name: 'No, click me' })).toBe(null);
42
- }}
43
- />
44
- </Test>
45
- </Describe>
46
- </Describe>
@@ -1,229 +0,0 @@
1
- import { SourceMapConsumer } from 'source-map';
2
- import getPlugins from './vitest.js';
3
-
4
- const dummyTestFile = `
5
- <script>
6
- import { Describe, Test, Check } from '../components/core';
7
- </script>
8
- <Describe label="A test suite">
9
- <Test it="A test case">
10
- <Check fn={() => {}} />
11
- </Test>
12
- <Describe label="A nested test suite">
13
- <Test it="A nested test case">
14
- <Check fn={() => {}} />
15
- </Test>
16
- </Describe>
17
- </Describe>
18
- <Describe label="Another test suite">
19
- <Test it="Another test case">
20
- <Check fn={() => {}} />
21
- </Test>
22
- </Describe>
23
- `;
24
-
25
- const findLineAndColumn = (code: string, substring: string) => {
26
- const index = code.indexOf(substring);
27
- if (index === -1) {
28
- throw new Error(`Substring "${substring}" not found in code.`);
29
- }
30
-
31
- const lines = code.slice(0, index).split('\n');
32
- const line = lines.length;
33
- const column = lines[lines.length - 1].length + 1;
34
-
35
- return { line, column };
36
- };
37
-
38
- describe('vitest pre plugin', () => {
39
- it('does not transform non-test files', async () => {
40
- const [pre] = getPlugins();
41
-
42
- expect(await pre.transform(`<h1>Not a test file</h1>`, 'AComponent.svelte')).toBeUndefined();
43
- });
44
-
45
- it('transforms .test.svelte files', async () => {
46
- const [pre] = getPlugins();
47
-
48
- expect(await pre.transform(dummyTestFile, 'AComponent.test.svelte')).not.toBeUndefined();
49
- });
50
-
51
- it('transforms .spec.svelte files', async () => {
52
- const [pre] = getPlugins();
53
-
54
- expect(await pre.transform(dummyTestFile, 'AComponent.spec.svelte')).not.toBeUndefined();
55
- });
56
-
57
- it('produces code with a dummy test suite', async () => {
58
- const [pre] = getPlugins();
59
- const result = await pre.transform(dummyTestFile, 'AComponent.test.svelte');
60
-
61
- expect(result?.code).toMatchSnapshot();
62
- });
63
-
64
- it('produces a source map with mappings that point to the Describe and Test components', async () => {
65
- const [pre] = getPlugins();
66
- const result = (await pre.transform(dummyTestFile, 'AComponent.test.svelte')) as {
67
- code: string;
68
- map: string;
69
- };
70
-
71
- const map = await new SourceMapConsumer(result?.map);
72
- const describePosition = findLineAndColumn(result.code, 'describe("A test suite"');
73
- const testPosition = findLineAndColumn(result.code, 'test("A test case"');
74
- const nestedDescribePosition = findLineAndColumn(result.code, 'describe("A nested test suite"');
75
- const nestedTestPosition = findLineAndColumn(result.code, 'test("A nested test case"');
76
- const anotherDescribePosition = findLineAndColumn(result.code, 'describe("Another test suite"');
77
- const anotherTestPosition = findLineAndColumn(result.code, 'test("Another test case"');
78
-
79
- expect(map.originalPositionFor(describePosition)).toEqual(
80
- expect.objectContaining({
81
- line: 5,
82
- column: 1,
83
- }),
84
- );
85
- expect(map.originalPositionFor(testPosition)).toEqual(
86
- expect.objectContaining({
87
- line: 6,
88
- column: 3,
89
- }),
90
- );
91
- expect(map.originalPositionFor(nestedDescribePosition)).toEqual(
92
- expect.objectContaining({
93
- line: 9,
94
- column: 3,
95
- }),
96
- );
97
- expect(map.originalPositionFor(nestedTestPosition)).toEqual(
98
- expect.objectContaining({
99
- line: 10,
100
- column: 5,
101
- }),
102
- );
103
- expect(map.originalPositionFor(anotherDescribePosition)).toEqual(
104
- expect.objectContaining({
105
- line: 15,
106
- column: 1,
107
- }),
108
- );
109
- expect(map.originalPositionFor(anotherTestPosition)).toEqual(
110
- expect.objectContaining({
111
- line: 16,
112
- column: 3,
113
- }),
114
- );
115
- });
116
-
117
- it('produces (unnamed) suite names for Describe', async () => {
118
- const [pre] = getPlugins();
119
- const result = (await pre.transform(
120
- `
121
- <Describe>
122
- <Test it="A test case">
123
- <Check fn={() => {}} />
124
- </Test>
125
- </Describe>
126
- `,
127
- 'AComponent.test.svelte',
128
- )) as { code: string };
129
-
130
- expect(result.code).toContain('describe("(unnamed)"');
131
- });
132
-
133
- it('produces (unnamed) test names for Test', async () => {
134
- const [pre] = getPlugins();
135
- const result = (await pre.transform(
136
- `
137
- <Describe label="A test suite">
138
- <Test>
139
- <Check fn={() => {}} />
140
- </Test>
141
- </Describe>
142
- `,
143
- 'AComponent.test.svelte',
144
- )) as { code: string };
145
-
146
- expect(result.code).toContain('test("(unnamed)"');
147
- });
148
-
149
- it('produces (unnamed) suite names when Describe has dynamic label', async () => {
150
- const [pre] = getPlugins();
151
- const result = (await pre.transform(
152
- `
153
- <Describe label={someVariable}>
154
- <Test it="A test case">
155
- <Check fn={() => {}} />
156
- </Test>
157
- </Describe>
158
- `,
159
- 'AComponent.test.svelte',
160
- )) as { code: string };
161
-
162
- expect(result.code).toContain('describe("(unnamed)"');
163
- });
164
-
165
- it('produces (unnamed) test names when Test has dynamic name', async () => {
166
- const [pre] = getPlugins();
167
- const result = (await pre.transform(
168
- `
169
- <Describe label="A test suite">
170
- <Test it={someVariable}>
171
- <Check fn={() => {}} />
172
- </Test>
173
- </Describe>
174
- `,
175
- 'AComponent.test.svelte',
176
- )) as { code: string };
177
-
178
- expect(result.code).toContain('test("(unnamed)"');
179
- });
180
-
181
- it('does not produce descibe or test calls when no Describe or Test components are present', async () => {
182
- const [pre] = getPlugins();
183
- const result = (await pre.transform(
184
- `
185
- <Header>Not a test file</Header>
186
- `,
187
- 'AComponent.test.svelte',
188
- )) as { code: string };
189
-
190
- expect(result.code).not.toContain('describe(');
191
- expect(result.code).not.toContain('test(');
192
- });
193
- });
194
-
195
- describe('vitest post plugin', () => {
196
- it('does not transform non-test files', async () => {
197
- const [, post] = getPlugins();
198
-
199
- expect(
200
- await post.transform(`export default function AComponent() {}`, 'AComponent.svelte'),
201
- ).toBeUndefined();
202
- });
203
-
204
- it('transforms .test.svelte files to mount the component', async () => {
205
- const [, post] = getPlugins();
206
-
207
- expect(
208
- await post.transform(`export default function AComponent() {}`, 'AComponent.test.svelte'),
209
- ).not.toBeUndefined();
210
- });
211
-
212
- it('transforms .spec.svelte files to mount the component', async () => {
213
- const [, post] = getPlugins();
214
-
215
- expect(
216
- await post.transform(`export default function AComponent() {}`, 'AComponent.spec.svelte'),
217
- ).not.toBeUndefined();
218
- });
219
-
220
- it('produces code that mounts the component and returns a render result', async () => {
221
- const [, post] = getPlugins();
222
- const result = await post.transform(
223
- `export default function AComponent() {}`,
224
- 'AComponent.test.svelte',
225
- );
226
-
227
- expect(result?.code).toMatchSnapshot();
228
- });
229
- });
package/tsconfig.json DELETED
@@ -1,35 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "lib": ["DOM", "ESNext"],
4
- "types": ["vitest/globals"],
5
- "noEmit": true,
6
- "rewriteRelativeImportExtensions": true,
7
- "allowJs": true,
8
- "checkJs": true,
9
- "esModuleInterop": true,
10
- "forceConsistentCasingInFileNames": true,
11
- "resolveJsonModule": true,
12
- "skipLibCheck": true,
13
- "sourceMap": true,
14
- "strict": true,
15
- "module": "preserve",
16
- "moduleResolution": "bundler"
17
- },
18
- "include": [
19
- "./types/**/$types.d.ts",
20
- "./vite.config.js",
21
- "./vite.config.ts",
22
- "./src/**/*.js",
23
- "./src/**/*.ts",
24
- "./src/**/*.svelte",
25
- "./tests/**/*.js",
26
- "./tests/**/*.ts",
27
- "./tests/**/*.svelte"
28
- ],
29
- "exclude": ["./node_modules/**"]
30
- // Path aliases are handled by https://svelte.dev/docs/kit/configuration#alias
31
- // except $lib which is handled by https://svelte.dev/docs/kit/configuration#files
32
- //
33
- // To make changes to top-level options such as include and exclude, we recommend extending
34
- // the generated config; see https://svelte.dev/docs/kit/configuration#typescript
35
- }
package/vitest.config.ts DELETED
@@ -1,21 +0,0 @@
1
- import { defineConfig } from 'vitest/config';
2
- import { svelteTesting } from '@testing-library/svelte/vite';
3
- import { svelte } from '@sveltejs/vite-plugin-svelte';
4
- import getPlugins from './src/plugins/vitest';
5
-
6
- export default defineConfig({
7
- logLevel: 'warn',
8
- plugins: [svelte(), svelteTesting(), getPlugins()],
9
- test: {
10
- coverage: {
11
- provider: 'v8',
12
- reporter: ['text', 'html'],
13
- },
14
- environment: 'happy-dom',
15
- expect: {
16
- requireAssertions: true,
17
- },
18
- globals: true,
19
- include: ['examples/**/*.{test,spec}.svelte', 'src/**/*.{test,spec}.ts'],
20
- },
21
- });