Skip to content

Commit 231e441

Browse files
committed
[bug: #19, #22] Fix examples and v27 Jest support / ESM
1 parent 01c1d92 commit 231e441

9 files changed

+143
-37
lines changed

README.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ After the preset runs,
2828

2929
### ⚙️ Options
3030

31-
| Description | Flag | Negated | Default |
32-
| ------------------ | --------------- | ------------------ | ------- |
33-
| Interactive Mode | `--interaction` | `--no-interaction` | True |
34-
| Jest DOM Support | `--jest-dom` | `--no-jest-dom` | True |
35-
| Typescript Support | `--ts` | `--no-ts` | False |
36-
| Generate Example | `--examples` | `--no-examples` | True |
31+
| Description | Flag | Negated | Default |
32+
| ------------------------- | --------------- | ------------------ | ------- |
33+
| Interactive Mode | `--interaction` | `--no-interaction` | True |
34+
| Jest DOM Support | `--jest-dom` | `--no-jest-dom` | True |
35+
| Typescript Support | `--ts` | `--no-ts` | False |
36+
| JSDOM Jest Env by Default | `--jsdom` | `--jsdom` | True |
37+
| Generate Example | `--examples` | `--no-examples` | True |
3738

3839
### 📑 Relevant Documentation
3940

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": true,
33
"name": "@rossyman/svelte-add-jest",
4-
"version": "1.2.0",
4+
"version": "1.3.0",
55
"description": "SvelteKit adder for Jest unit testing",
66
"license": "MIT",
77
"keywords": [

preset.ts

+31-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ type Dependencies = {
44
[key: string]: {
55
version: string;
66
type?: 'DEV' | 'PEER';
7-
reliesOn?: string;
7+
reliesOn?: string | string[];
88
}
99
}
1010

@@ -126,7 +126,9 @@ abstract class Adder {
126126
}
127127

128128
action.if(() => dependencyConfig.reliesOn
129-
? this.getConfiguration(dependencyConfig.reliesOn)
129+
? Array.isArray(dependencyConfig.reliesOn)
130+
? dependencyConfig.reliesOn.every(Boolean)
131+
: this.getConfiguration(dependencyConfig.reliesOn)
130132
: true
131133
);
132134
});
@@ -158,7 +160,8 @@ class SvelteJestAdder extends Adder {
158160
protected readonly CONFIGURATION: Configuration = {
159161
'jest-dom': {message: 'Enable Jest DOM support?', default: true, question: true},
160162
'ts': {message: 'Enable TypeScript support?', default: false, question: true},
161-
'examples': {message: 'Generate example test file?', default: true, question: true}
163+
'examples': {message: 'Generate example test file?', default: true, question: true},
164+
'jsdom': {message: 'Enable JSDOM environment by default?', default: true, question: true}
162165
};
163166

164167
protected readonly REQUIRED_DEPENDENCIES: Dependencies = {
@@ -171,7 +174,7 @@ class SvelteJestAdder extends Adder {
171174
'@testing-library/jest-dom': {version: '^5.14.0', type: 'DEV', reliesOn: 'jest-dom'},
172175
'ts-jest': {version: '^27.0.0', type: 'DEV', reliesOn: 'ts'},
173176
'@types/jest': {version: '^27.0.0', type: 'DEV', reliesOn: 'ts'},
174-
'@types/testing-library__jest-dom': {version: '^5.14.0', type: 'DEV', reliesOn: 'ts'}
177+
'@types/testing-library__jest-dom': {version: '^5.14.0', type: 'DEV', reliesOn: ['ts', 'jest-dom']}
175178
};
176179

177180
run(): void {
@@ -197,33 +200,50 @@ class SvelteJestAdder extends Adder {
197200
Preset
198201
.editJson('jest.config.json').merge({
199202
transform: {
200-
'^.+\\.svelte$': ['svelte-jester', {preprocess: true}],
203+
'^.+\\.svelte$': ['./node_modules/svelte-jester/dist/transformer.mjs', {preprocess: true}],
201204
'^.+\\.ts$': 'ts-jest'
202205
},
203-
moduleFileExtensions: ['js', 'ts', 'svelte'],
206+
moduleFileExtensions: ['ts'],
207+
extensionsToTreatAsEsm: ['.ts'],
204208
globals: {
205209
'ts-jest': {
206-
tsconfig: 'tsconfig.spec.json'
210+
tsconfig: 'tsconfig.spec.json',
211+
'useESM': true
207212
}
208213
}
209214
})
210215
.withTitle('Modifying Jest config for TypeScript transformation')
211216
.if(() => this.getConfiguration('ts'));
212217

218+
Preset
219+
.editJson('jest.config.json').merge({
220+
testEnvironment: 'jsdom'
221+
})
222+
.withTitle('Modifying Jest config to enable JSDOM environment')
223+
.if(() => this.getConfiguration('jsdom'));
224+
213225
this.safeExtract('Initializing TypeScript config for tests', 'tsconfig.spec.json')
214226
.if(() => this.getConfiguration('ts'));
215227

228+
this.safeExtract('Initializing example test file', 'index.spec.ts')
229+
.to('src/routes/')
230+
.if(() => this.getConfiguration('examples') && this.getConfiguration('ts') && !this.getConfiguration('jest-dom'));
231+
216232
this.safeExtract('Initializing example test file', 'index.spec.js')
217233
.to('src/routes/')
218-
.if(() => this.getConfiguration('examples') && this.getConfiguration('jest-dom') && !this.getConfiguration('ts'));
234+
.if(() => this.getConfiguration('examples') && !this.getConfiguration('ts') && !this.getConfiguration('jest-dom'));
219235

220-
this.safeExtract('Initializing example test file', 'index.spec.ts')
236+
this.safeExtract('Initializing example test file', 'index-dom.spec.ts')
237+
.to('src/routes/')
238+
.if(() => this.getConfiguration('examples') && this.getConfiguration('ts') && this.getConfiguration('jest-dom'));
239+
240+
this.safeExtract('Initializing example test file', 'index-dom.spec.js')
221241
.to('src/routes/')
222-
.if(() => this.getConfiguration('examples') && this.getConfiguration('jest-dom') && this.getConfiguration('ts'));
242+
.if(() => this.getConfiguration('examples') && !this.getConfiguration('ts') && this.getConfiguration('jest-dom'));
223243

224244
Preset
225245
.editJson('package.json')
226-
.merge({scripts: {'test': 'jest src --config jest.config.json', 'test:watch': 'npm run test -- --watch'}})
246+
.merge({scripts: {'test': 'NODE_OPTIONS=--experimental-vm-modules jest src --config jest.config.json', 'test:watch': 'npm run test -- --watch'}})
227247
.withTitle('Adding test scripts to package.json');
228248

229249
Preset

templates/index-dom.spec.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { render } from '@testing-library/svelte';
2+
import Index from './index.svelte';
3+
4+
/**
5+
* @jest-environment jsdom
6+
*/
7+
8+
/**
9+
* An example test suite outlining the usage of
10+
* `describe()`, `beforeEach()`, `test()` and `expect()`
11+
*
12+
* @see https://jestjs.io/docs/getting-started
13+
* @see https://github.com/testing-library/jest-dom
14+
*/
15+
16+
describe('Index', () => {
17+
18+
let renderedComponent;
19+
20+
beforeEach(() => {
21+
renderedComponent = render(Index);
22+
});
23+
24+
describe('once the component has been rendered', () => {
25+
26+
test('should show the proper heading', () => {
27+
expect(renderedComponent.getByText('Welcome to SvelteKit')).toBeInTheDocument();
28+
});
29+
30+
});
31+
32+
});

templates/index-dom.spec.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { render, RenderResult } from '@testing-library/svelte';
2+
import Index from './index.svelte';
3+
4+
/**
5+
* @jest-environment jsdom
6+
*/
7+
8+
/**
9+
* An example test suite outlining the usage of
10+
* `describe()`, `beforeEach()`, `test()` and `expect()`
11+
*
12+
* @see https://jestjs.io/docs/getting-started
13+
* @see https://github.com/testing-library/jest-dom
14+
*/
15+
16+
describe('Index', () => {
17+
18+
let renderedComponent: RenderResult;
19+
20+
beforeEach(() => {
21+
renderedComponent = render(Index);
22+
});
23+
24+
describe('once the component has been rendered', () => {
25+
26+
test('should show the proper heading', () => {
27+
expect(renderedComponent.getByText('Welcome to SvelteKit')).toBeInTheDocument();
28+
});
29+
30+
});
31+
32+
});

templates/index.spec.js

+19-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
/**
2-
* @jest-environment jsdom
2+
* An example test suite outlining the usage of
3+
* `describe()`, `beforeEach()`, `test()` and `expect()`
4+
*
5+
* @see https://jestjs.io/docs/getting-started
36
*/
47

5-
import '@testing-library/jest-dom/extend-expect';
6-
import { render } from '@testing-library/svelte';
7-
import index from './index.svelte';
8+
describe('Index', () => {
9+
10+
let isTestSuitePassing = false;
11+
12+
beforeEach(() => {
13+
isTestSuitePassing = true;
14+
});
15+
16+
describe('isTestSuitePassing', () => {
17+
18+
test('should be true', () => {
19+
expect(isTestSuitePassing).toBe(true);
20+
});
21+
22+
});
823

9-
test('shows proper heading when rendered', () => {
10-
const { getByText } = render(index);
11-
expect(getByText('Hello world!')).toBeInTheDocument();
1224
});

templates/index.spec.ts

+19-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
/**
2-
* @jest-environment jsdom
2+
* An example test suite outlining the usage of
3+
* `describe()`, `beforeEach()`, `test()` and `expect()`
4+
*
5+
* @see https://jestjs.io/docs/getting-started
36
*/
47

5-
import '@testing-library/jest-dom/extend-expect';
6-
import { render } from '@testing-library/svelte';
7-
import index from './index.svelte';
8+
describe('Index', () => {
9+
10+
let isTestSuitePassing = false;
11+
12+
beforeEach(() => {
13+
isTestSuitePassing = true;
14+
});
15+
16+
describe('isTestSuitePassing', () => {
17+
18+
test('should be true', () => {
19+
expect(isTestSuitePassing).toBe(true);
20+
});
21+
22+
});
823

9-
test('shows proper heading when rendered', () => {
10-
const { getByText } = render(index);
11-
expect(getByText('Hello world!')).toBeInTheDocument();
1224
});

templates/jest.config.json

+1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
"^\\$lib(.*)$": "<rootDir>/src/lib$1",
88
"^\\$app(.*)$": ["<rootDir>/.svelte-kit/dev/runtime/app$1", "<rootDir>/.svelte-kit/build/runtime/app$1"]
99
},
10+
"extensionsToTreatAsEsm": [".svelte"],
1011
"moduleFileExtensions": ["js", "svelte"]
1112
}

templates/tsconfig.spec.json

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
{
22
"extends": "./tsconfig.json",
33
"compilerOptions": {
4-
"isolatedModules": false,
5-
"lib": [
6-
"es2020",
7-
"DOM"
8-
]
4+
"isolatedModules": false
95
},
106
"exclude": [
117
"src/**"

0 commit comments

Comments
 (0)