From f2282de763446441f29abd79ac4db10566154d7d Mon Sep 17 00:00:00 2001 From: Josh GM Walker <56300765+Josh-Walker-GM@users.noreply.github.com> Date: Sat, 20 Jan 2024 14:30:21 +0000 Subject: [PATCH] chore(crwa): switch to vitest (#9855) Continuing the exploration of switching packages over to vitest. Notes: 1. The `test:e2e` required a small change to the default vitest options. We have to add `--pool=forks` (see: https://vitest.dev/config/#pool-1-0-0 for the pool option). This is because we're using zx to do things like change directories. This is not possible with the default `threads` pool. See: https://github.com/vitest-dev/vitest/issues/566 for more information related to the specific error that I encountered. --------- Co-authored-by: Dominic Saadi --- .github/workflows/ci.yml | 2 +- packages/create-redwood-app/jest.config.js | 5 ----- packages/create-redwood-app/package.json | 5 +++-- .../create-redwood-app/tests/{e2e.test.js => e2e.test.ts} | 6 +++++- .../tests/{templates.test.js => templates.test.ts} | 3 ++- packages/create-redwood-app/vitest.config.mts | 7 +++++++ yarn.lock | 1 + 7 files changed, 19 insertions(+), 10 deletions(-) delete mode 100644 packages/create-redwood-app/jest.config.js rename packages/create-redwood-app/tests/{e2e.test.js => e2e.test.ts} (94%) rename packages/create-redwood-app/tests/{templates.test.js => templates.test.ts} (98%) create mode 100644 packages/create-redwood-app/vitest.config.mts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 88060543a401..03553e30267f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -848,7 +848,7 @@ jobs: git config --global user.name "Your Name" - name: e2e test - run: yarn test e2e + run: yarn test:e2e working-directory: ./packages/create-redwood-app env: PROJECT_PATH: ${{ env.PROJECT_PATH }} diff --git a/packages/create-redwood-app/jest.config.js b/packages/create-redwood-app/jest.config.js deleted file mode 100644 index 99951968fbc0..000000000000 --- a/packages/create-redwood-app/jest.config.js +++ /dev/null @@ -1,5 +0,0 @@ -/** @type {import('jest').Config} */ -export default { - testPathIgnorePatterns: ['/node_modules/', '/templates/'], - transform: {}, -} diff --git a/packages/create-redwood-app/package.json b/packages/create-redwood-app/package.json index 1ea46667c57b..73cba0ca9ed7 100644 --- a/packages/create-redwood-app/package.json +++ b/packages/create-redwood-app/package.json @@ -19,8 +19,8 @@ "build:watch": "nodemon --watch src --ignore dist,template --exec \"yarn build\"", "prepublishOnly": "NODE_ENV=production yarn build", "set-up-test-project": "node ./scripts/setUpTestProject.js", - "test": "node --experimental-vm-modules $(yarn bin jest) templates", - "test:e2e": "node --experimental-vm-modules $(yarn bin jest) e2e", + "test": "vitest run templates", + "test:e2e": "vitest --pool=forks run e2e", "ts-to-js": "yarn node ./scripts/tsToJS.js" }, "devDependencies": { @@ -47,6 +47,7 @@ "terminal-link": "2.1.1", "untildify": "4.0.0", "uuid": "9.0.1", + "vitest": "1.2.1", "yargs": "17.7.2" }, "gitHead": "3905ed045508b861b495f8d5630d76c7a157d8f1" diff --git a/packages/create-redwood-app/tests/e2e.test.js b/packages/create-redwood-app/tests/e2e.test.ts similarity index 94% rename from packages/create-redwood-app/tests/e2e.test.js rename to packages/create-redwood-app/tests/e2e.test.ts index 7ddee4eace96..50ea12a89793 100644 --- a/packages/create-redwood-app/tests/e2e.test.js +++ b/packages/create-redwood-app/tests/e2e.test.ts @@ -1,7 +1,11 @@ /* eslint-env node */ +import { describe, test, expect, it } from 'vitest' import { cd, fs, $ } from 'zx' +if (!process.env.PROJECT_PATH) { + throw new Error('PROJECT_PATH environment variable is not set') +} const projectPath = await fs.realpath(process.env.PROJECT_PATH) cd(projectPath) @@ -83,7 +87,7 @@ describe('create-redwood-app', () => { await fs.rm('./redwood-app', { recursive: true, force: true }) }) - it.failing('fails on unknown options', async () => { + it.fails('fails on unknown options', async () => { try { await $`yarn create-redwood-app --unknown-options`.timeout(2500) // Fail the test if the function didn't throw. diff --git a/packages/create-redwood-app/tests/templates.test.js b/packages/create-redwood-app/tests/templates.test.ts similarity index 98% rename from packages/create-redwood-app/tests/templates.test.js rename to packages/create-redwood-app/tests/templates.test.ts index 667011a2c6f8..be70e2c81a01 100644 --- a/packages/create-redwood-app/tests/templates.test.js +++ b/packages/create-redwood-app/tests/templates.test.ts @@ -2,6 +2,7 @@ import path from 'node:path' import { fileURLToPath } from 'node:url' import klawSync from 'klaw-sync' +import { describe, it, expect } from 'vitest' const TS_TEMPLATE_DIR = fileURLToPath( new URL('../templates/ts', import.meta.url) @@ -192,7 +193,7 @@ describe('JS template', () => { * @returns string[] */ function getDirectoryStructure(dir) { - let fileStructure = klawSync(dir) + const fileStructure = klawSync(dir) return fileStructure .filter( diff --git a/packages/create-redwood-app/vitest.config.mts b/packages/create-redwood-app/vitest.config.mts new file mode 100644 index 000000000000..6d9f9224671e --- /dev/null +++ b/packages/create-redwood-app/vitest.config.mts @@ -0,0 +1,7 @@ +import { defineConfig, configDefaults } from 'vitest/config' + +export default defineConfig({ + test: { + exclude: [...configDefaults.exclude, 'templates/**'], + }, +}) diff --git a/yarn.lock b/yarn.lock index 168ef346f123..715a8c3665c1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -15992,6 +15992,7 @@ __metadata: terminal-link: "npm:2.1.1" untildify: "npm:4.0.0" uuid: "npm:9.0.1" + vitest: "npm:1.2.1" yargs: "npm:17.7.2" bin: create-redwood-app: ./dist/create-redwood-app.js