From 6a2ca342f81f375a0151665938cef3cb846196e9 Mon Sep 17 00:00:00 2001 From: Ross MacPhee <10212476+rossyman@users.noreply.github.com> Date: Thu, 9 Sep 2021 20:23:34 +0100 Subject: [PATCH] [enhancement: #14] Compatibility with Vite import.meta --- package.json | 2 +- preset.ts | 7 +++++-- templates/.babelrc | 13 ++----------- templates/index-dom.spec.js | 2 +- templates/index-dom.spec.ts | 2 +- templates/index.spec.js | 17 ++++++++++++----- templates/index.spec.ts | 17 ++++++++++++----- templates/jest.config.json | 5 ++++- 8 files changed, 38 insertions(+), 27 deletions(-) diff --git a/package.json b/package.json index 0d59257..d4697da 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "private": true, "name": "@rossyman/svelte-add-jest", - "version": "1.3.0", + "version": "1.3.1", "description": "SvelteKit adder for Jest unit testing", "license": "MIT", "keywords": [ diff --git a/preset.ts b/preset.ts index 1e88537..b46b5c4 100644 --- a/preset.ts +++ b/preset.ts @@ -166,8 +166,8 @@ class SvelteJestAdder extends Adder { protected readonly REQUIRED_DEPENDENCIES: Dependencies = { '@babel/core': {version: '^7.14.0', type: 'DEV'}, - '@babel/preset-env': {version: '^7.14.0', type: 'DEV'}, 'jest': {version: '^27.0.0', type: 'DEV'}, + 'babel-plugin-transform-vite-meta-env': {version: '^1.0.1', type: 'DEV'}, 'babel-jest': {version: '^27.0.0', type: 'DEV'}, 'svelte-jester': {version: '^2.0.1', type: 'DEV'}, '@testing-library/svelte': {version: '^3.0.0', type: 'DEV'}, @@ -243,7 +243,10 @@ class SvelteJestAdder extends Adder { Preset .editJson('package.json') - .merge({scripts: {'test': 'NODE_OPTIONS=--experimental-vm-modules 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 diff --git a/templates/.babelrc b/templates/.babelrc index 522c7e1..b5fe3b4 100644 --- a/templates/.babelrc +++ b/templates/.babelrc @@ -1,12 +1,3 @@ { - "presets": [ - [ - "@babel/preset-env", - { - "targets": { - "node": "current" - } - } - ] - ] -} \ No newline at end of file + "plugins": ["babel-plugin-transform-vite-meta-env"] +} diff --git a/templates/index-dom.spec.js b/templates/index-dom.spec.js index 433f150..be6008d 100644 --- a/templates/index-dom.spec.js +++ b/templates/index-dom.spec.js @@ -24,7 +24,7 @@ describe('Index', () => { describe('once the component has been rendered', () => { test('should show the proper heading', () => { - expect(renderedComponent.getByText('Welcome to SvelteKit')).toBeInTheDocument(); + expect(renderedComponent.getByText('SvelteKit', {exact: false})).toBeInTheDocument(); }); }); diff --git a/templates/index-dom.spec.ts b/templates/index-dom.spec.ts index c7883b1..1e08197 100644 --- a/templates/index-dom.spec.ts +++ b/templates/index-dom.spec.ts @@ -24,7 +24,7 @@ describe('Index', () => { describe('once the component has been rendered', () => { test('should show the proper heading', () => { - expect(renderedComponent.getByText('Welcome to SvelteKit')).toBeInTheDocument(); + expect(renderedComponent.getByText('SvelteKit', {exact: false})).toBeInTheDocument(); }); }); diff --git a/templates/index.spec.js b/templates/index.spec.js index 5c601bb..168728e 100644 --- a/templates/index.spec.js +++ b/templates/index.spec.js @@ -1,3 +1,10 @@ +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()` @@ -7,16 +14,16 @@ describe('Index', () => { - let isTestSuitePassing = false; + let renderedComponent; beforeEach(() => { - isTestSuitePassing = true; + renderedComponent = render(Index); }); - describe('isTestSuitePassing', () => { + describe('once the component has been rendered', () => { - test('should be true', () => { - expect(isTestSuitePassing).toBe(true); + test('should show the proper heading', () => { + expect(renderedComponent.getByText('SvelteKit', {exact: false})).toBeDefined(); }); }); diff --git a/templates/index.spec.ts b/templates/index.spec.ts index 5c601bb..2523dce 100644 --- a/templates/index.spec.ts +++ b/templates/index.spec.ts @@ -1,3 +1,10 @@ +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()` @@ -7,16 +14,16 @@ describe('Index', () => { - let isTestSuitePassing = false; + let renderedComponent: RenderResult; beforeEach(() => { - isTestSuitePassing = true; + renderedComponent = render(Index); }); - describe('isTestSuitePassing', () => { + describe('once the component has been rendered', () => { - test('should be true', () => { - expect(isTestSuitePassing).toBe(true); + test('should show the proper heading', () => { + expect(renderedComponent.getByText('SvelteKit', {exact: false})).toBeDefined(); }); }); diff --git a/templates/jest.config.json b/templates/jest.config.json index 941a4f9..aa038af 100644 --- a/templates/jest.config.json +++ b/templates/jest.config.json @@ -5,7 +5,10 @@ }, "moduleNameMapper": { "^\\$lib(.*)$": "/src/lib$1", - "^\\$app(.*)$": ["/.svelte-kit/dev/runtime/app$1", "/.svelte-kit/build/runtime/app$1"] + "^\\$app(.*)$": [ + "/.svelte-kit/dev/runtime/app$1", + "/.svelte-kit/build/runtime/app$1" + ] }, "extensionsToTreatAsEsm": [".svelte"], "moduleFileExtensions": ["js", "svelte"]