Skip to content

Commit

Permalink
[bug: #19, #22] Fix examples and v27 Jest support / ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
rossyman committed Sep 9, 2021
1 parent 01c1d92 commit bd18366
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 37 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ After the preset runs,

### ⚙️ Options

| Description | Flag | Negated | Default |
| ------------------ | --------------- | ------------------ | ------- |
| Interactive Mode | `--interaction` | `--no-interaction` | True |
| Jest DOM Support | `--jest-dom` | `--no-jest-dom` | True |
| Typescript Support | `--ts` | `--no-ts` | False |
| Generate Example | `--examples` | `--no-examples` | True |
| Description | Flag | Negated | Default |
| ------------------------- | --------------- | ------------------ | ------- |
| Interactive Mode | `--interaction` | `--no-interaction` | True |
| Jest DOM Support | `--jest-dom` | `--no-jest-dom` | True |
| Typescript Support | `--ts` | `--no-ts` | False |
| JSDOM Jest Env by Default | `--jsdom` | `--jsdom` | True |
| Generate Example | `--examples` | `--no-examples` | True |

### 📑 Relevant Documentation

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "@rossyman/svelte-add-jest",
"version": "1.2.0",
"version": "1.3.0",
"description": "SvelteKit adder for Jest unit testing",
"license": "MIT",
"keywords": [
Expand Down
42 changes: 31 additions & 11 deletions preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type Dependencies = {
[key: string]: {
version: string;
type?: 'DEV' | 'PEER';
reliesOn?: string;
reliesOn?: string | string[];
}
}

Expand Down Expand Up @@ -126,7 +126,9 @@ abstract class Adder {
}

action.if(() => dependencyConfig.reliesOn
? this.getConfiguration(dependencyConfig.reliesOn)
? Array.isArray(dependencyConfig.reliesOn)
? dependencyConfig.reliesOn.every(Boolean)
: this.getConfiguration(dependencyConfig.reliesOn)
: true
);
});
Expand Down Expand Up @@ -158,7 +160,8 @@ class SvelteJestAdder extends Adder {
protected readonly CONFIGURATION: Configuration = {
'jest-dom': {message: 'Enable Jest DOM support?', default: true, question: true},
'ts': {message: 'Enable TypeScript support?', default: false, question: true},
'examples': {message: 'Generate example test file?', default: true, question: true}
'examples': {message: 'Generate example test file?', default: true, question: true},
'jsdom': {message: 'Enable JSDOM environment by default?', default: true, question: true}
};

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

run(): void {
Expand All @@ -197,33 +200,50 @@ class SvelteJestAdder extends Adder {
Preset
.editJson('jest.config.json').merge({
transform: {
'^.+\\.svelte$': ['svelte-jester', {preprocess: true}],
'^.+\\.svelte$': ['./node_modules/svelte-jester/dist/transformer.mjs', {preprocess: true}],
'^.+\\.ts$': 'ts-jest'
},
moduleFileExtensions: ['js', 'ts', 'svelte'],
moduleFileExtensions: ['ts'],
extensionsToTreatAsEsm: ['.ts'],
globals: {
'ts-jest': {
tsconfig: 'tsconfig.spec.json'
tsconfig: 'tsconfig.spec.json',
'useESM': true
}
}
})
.withTitle('Modifying Jest config for TypeScript transformation')
.if(() => this.getConfiguration('ts'));

Preset
.editJson('jest.config.json').merge({
testEnvironment: 'jsdom'
})
.withTitle('Modifying Jest config to enable JSDOM environment')
.if(() => this.getConfiguration('jsdom'));

this.safeExtract('Initializing TypeScript config for tests', 'tsconfig.spec.json')
.if(() => this.getConfiguration('ts'));

this.safeExtract('Initializing example test file', 'index.spec.ts')
.to('src/routes/')
.if(() => this.getConfiguration('examples') && this.getConfiguration('ts') && !this.getConfiguration('jest-dom'));

this.safeExtract('Initializing example test file', 'index.spec.js')
.to('src/routes/')
.if(() => this.getConfiguration('examples') && this.getConfiguration('jest-dom') && !this.getConfiguration('ts'));
.if(() => this.getConfiguration('examples') && !this.getConfiguration('ts') && !this.getConfiguration('jest-dom'));

this.safeExtract('Initializing example test file', 'index.spec.ts')
this.safeExtract('Initializing example test file', 'index-dom.spec.ts')
.to('src/routes/')
.if(() => this.getConfiguration('examples') && this.getConfiguration('ts') && this.getConfiguration('jest-dom'));

this.safeExtract('Initializing example test file', 'index-dom.spec.js')
.to('src/routes/')
.if(() => this.getConfiguration('examples') && this.getConfiguration('jest-dom') && this.getConfiguration('ts'));
.if(() => this.getConfiguration('examples') && !this.getConfiguration('ts') && this.getConfiguration('jest-dom'));

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

Preset
Expand Down
32 changes: 32 additions & 0 deletions templates/index-dom.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { render } from '@testing-library/svelte';
import Index from './index.svelte';

/**
* @jest-environment jsdom
*/

/**
* An example test suite outlining the usage of
* `describe()`, `beforeEach()`, `test()` and `expect()`
*
* @see https://jestjs.io/docs/getting-started
* @see https://github.com/testing-library/jest-dom
*/

describe('Index', () => {

let renderedComponent;

beforeEach(() => {
renderedComponent = render(Index);
});

describe('once the component has been rendered', () => {

test('should show the proper heading', () => {
expect(renderedComponent.getByText('Welcome to SvelteKit')).toBeInTheDocument();
});

});

});
32 changes: 32 additions & 0 deletions templates/index-dom.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { render, RenderResult } from '@testing-library/svelte';
import Index from './index.svelte';

/**
* @jest-environment jsdom
*/

/**
* An example test suite outlining the usage of
* `describe()`, `beforeEach()`, `test()` and `expect()`
*
* @see https://jestjs.io/docs/getting-started
* @see https://github.com/testing-library/jest-dom
*/

describe('Index', () => {

let renderedComponent: RenderResult;

beforeEach(() => {
renderedComponent = render(Index);
});

describe('once the component has been rendered', () => {

test('should show the proper heading', () => {
expect(renderedComponent.getByText('Welcome to SvelteKit')).toBeInTheDocument();
});

});

});
26 changes: 19 additions & 7 deletions templates/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
/**
* @jest-environment jsdom
* An example test suite outlining the usage of
* `describe()`, `beforeEach()`, `test()` and `expect()`
*
* @see https://jestjs.io/docs/getting-started
*/

import '@testing-library/jest-dom/extend-expect';
import { render } from '@testing-library/svelte';
import index from './index.svelte';
describe('Index', () => {

let isTestSuitePassing = false;

beforeEach(() => {
isTestSuitePassing = true;
});

describe('isTestSuitePassing', () => {

test('should be true', () => {
expect(isTestSuitePassing).toBe(true);
});

});

test('shows proper heading when rendered', () => {
const { getByText } = render(index);
expect(getByText('Hello world!')).toBeInTheDocument();
});
26 changes: 19 additions & 7 deletions templates/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
/**
* @jest-environment jsdom
* An example test suite outlining the usage of
* `describe()`, `beforeEach()`, `test()` and `expect()`
*
* @see https://jestjs.io/docs/getting-started
*/

import '@testing-library/jest-dom/extend-expect';
import { render } from '@testing-library/svelte';
import index from './index.svelte';
describe('Index', () => {

let isTestSuitePassing = false;

beforeEach(() => {
isTestSuitePassing = true;
});

describe('isTestSuitePassing', () => {

test('should be true', () => {
expect(isTestSuitePassing).toBe(true);
});

});

test('shows proper heading when rendered', () => {
const { getByText } = render(index);
expect(getByText('Hello world!')).toBeInTheDocument();
});
1 change: 1 addition & 0 deletions templates/jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"^\\$lib(.*)$": "<rootDir>/src/lib$1",
"^\\$app(.*)$": ["<rootDir>/.svelte-kit/dev/runtime/app$1", "<rootDir>/.svelte-kit/build/runtime/app$1"]
},
"extensionsToTreatAsEsm": [".svelte"],
"moduleFileExtensions": ["js", "svelte"]
}
6 changes: 1 addition & 5 deletions templates/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"isolatedModules": false,
"lib": [
"es2020",
"DOM"
]
"isolatedModules": false
},
"exclude": [
"src/**"
Expand Down

0 comments on commit bd18366

Please sign in to comment.