Skip to content

Commit

Permalink
chore(vite): switch to vitest (redwoodjs#9868)
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Walker-GM authored Jan 22, 2024
1 parent 8467a7c commit 1a1e758
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 64 deletions.
9 changes: 4 additions & 5 deletions packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@
"build:js": "babel src -d dist --extensions \".js,.jsx,.ts,.tsx\"",
"build:pack": "yarn pack -o redwoodjs-vite.tgz",
"build:types": "tsc --build --verbose",
"test": "yarn test:node && echo",
"test:node": "glob './src/**/__tests__/*.test.mts' --cmd='tsx --no-warnings --test'",
"test:watch": "glob './src/**/__tests__/*.test.mts' --cmd='tsx --no-warnings --test --watch'"
"test": "vitest run src",
"test:watch": "vitest watch src"
},
"dependencies": {
"@babel/runtime-corejs3": "7.23.6",
Expand Down Expand Up @@ -92,9 +91,9 @@
"@types/react": "18.2.37",
"@types/yargs-parser": "21.0.3",
"glob": "10.3.10",
"jest": "29.7.0",
"rollup": "3.29.4",
"typescript": "5.3.3"
"typescript": "5.3.3",
"vitest": "1.2.1"
},
"gitHead": "3905ed045508b861b495f8d5630d76c7a157d8f1"
}
46 changes: 10 additions & 36 deletions packages/vite/src/__tests__/viteNestedPages.test.mts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import assert from 'node:assert/strict'
import fs from 'node:fs'
import path from 'node:path'
import { test, after, before, beforeEach, describe, it } from 'node:test'
import { fileURLToPath } from 'url'

import { transformWithEsbuild } from 'vite'
import { test, describe, beforeEach, afterAll, beforeAll, it, expect, vi } from 'vitest'

import * as babel from '@babel/core'
import projectConfig from '@redwoodjs/project-config'
Expand Down Expand Up @@ -44,15 +43,14 @@ const __dirname = path.dirname(__filename)
const FIXTURE_PATH = path.join(__dirname, 'fixtures/nestedPages')
const { getPaths } = projectConfig

test('transform', async (t) => {
t.mock.method(fs, 'readFileSync', () => {
test('transform', async () => {
vi.spyOn(fs, 'readFileSync').mockImplementationOnce(() => {
return '<Router><Route path="/" page={HomePage} name="home" /></Router>'
})
vi.spyOn(fs, 'existsSync').mockImplementationOnce(() => true)

t.mock.method(fs, 'existsSync', () => true)

assert.equal(
await transform('Router.jsx'),
const transformed = await transform('Router.jsx')
expect(transformed).toEqual(
'/* @__PURE__ */ React.createElement(Router, null, /* @__PURE__ */ React.createElement(Route, { path: "/", page: HomePage, name: "home" }));\n'
)
})
Expand All @@ -65,11 +63,11 @@ describe('User specified imports, with static imports', () => {
process.env.RWJS_CWD = FIXTURE_PATH
})

after(() => {
afterAll(() => {
delete process.env.RWJS_CWD
})

before(async () => {
beforeAll(async () => {
process.env.RWJS_CWD = FIXTURE_PATH

const routesFile = getPaths().web.routes
Expand Down Expand Up @@ -231,9 +229,9 @@ describe('User specified imports, with static imports', () => {
})

it('Handles when imports from a page include non-default imports too', () => {
// Because we import import EditJobPage, 👉 { NonDefaultExport } from 'src/pages/Jobs/EditJobPage'
// Because we import import EditJobPage, 👉 { NonDefaultExport } from 'src/pages/Jobs/EditJobPage'

expect(outputWithStaticImports).toContain(
expect(outputWithStaticImports).toContain(
'import { NonDefaultExport } from "'
)

Expand All @@ -258,27 +256,3 @@ describe('User specified imports, with static imports', () => {
)
})
})

function expect(str) {
return {
toContain: (sub) => {
assert.equal(str.includes(sub), true, `Expected ${str} to contain ${sub}`)
},
not: {
toContain: (sub) => {
assert.equal(
str.includes(sub),
false,
`Expected ${str} to not contain ${sub}`
)
},
toMatch: (regex) => {
assert.equal(
regex.test(str),
false,
`Expected ${str} to not match ${regex}`
)
},
},
}
}
17 changes: 6 additions & 11 deletions packages/vite/src/plugins/__tests__/remove-from-bundle.test.mts
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import assert from 'node:assert/strict'
import { describe, it } from 'node:test'
import { describe, it, expect } from 'vitest'

import * as vitePluginRemoveFromBundle from '../vite-plugin-remove-from-bundle.js'

// @ts-expect-error We have to write it this way to appease node:test, but TS doesn't seem to like it.
// node:test needs to be configured correctly, I imagine.
const { excludeOnMatch } = vitePluginRemoveFromBundle.default
import { excludeOnMatch } from '../vite-plugin-remove-from-bundle'

describe('excludeModule', () => {
it('should return true if idToExclude matches id', () => {
const loadOutput = excludeOnMatch([{
id: /router\/dist\/splash-page/,
}],
'/Users/dac09/Experiments/splash-page-null-loader/node_modules/@redwoodjs/router/dist/splash-page.js?commonjs-exports',
'/Users/dac09/Experiments/splash-page-null-loader/node_modules/@redwoodjs/router/dist/splash-page.js?commonjs-exports',
)

assert.notStrictEqual(loadOutput, {
expect(loadOutput).not.toEqual({
code: 'module.exports = null'
})
})
Expand All @@ -24,8 +19,8 @@ describe('excludeModule', () => {
const loadOutput = excludeOnMatch([{
id: /bazinga-page/,
}],
'/Users/dac09/Experiments/splash-page-null-loader/node_modules/@redwoodjs/router/dist/params.js')
'/Users/dac09/Experiments/splash-page-null-loader/node_modules/@redwoodjs/router/dist/params.js')

assert.equal(loadOutput, null)
expect(loadOutput).toEqual(null)
})
})
18 changes: 7 additions & 11 deletions packages/vite/src/plugins/__tests__/swap-apollo-provider.test.mts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import assert from 'node:assert/strict'
import { describe, it } from 'node:test'

import plugin from '../vite-plugin-swap-apollo-provider.js'

// @ts-expect-error node test not configured correctly
const swapApolloProvider = plugin.default

import swapApolloProvider from '../vite-plugin-swap-apollo-provider'
import { describe, it, expect } from 'vitest'

describe('excludeModule', () => {
it('should swap the import', async() => {
it('should swap the import', async () => {
const plugin = swapApolloProvider()

const output = await plugin.transform(`import ApolloProvider from '@redwoodjs/web/apollo'`, '/Users/dac09/Experiments/ssr-2354/web/src/App.tsx')
// @ts-expect-error The PluginOption type is 'false | Plugin_2 | PluginOption[] | Promise<false | Plugin_2 | PluginOption[] | null | undefined>' which does not gaurentee that the transform method exists.
const output = await plugin.transform(`import ApolloProvider from '@redwoodjs/web/apollo'`, '/Users/dac09/Experiments/ssr-2354/web/src/App.tsx')

assert.strictEqual(output, "import ApolloProvider from '@redwoodjs/web/dist/apollo/suspense'")
})
expect(output).toEqual("import ApolloProvider from '@redwoodjs/web/dist/apollo/suspense'")
})
})
7 changes: 7 additions & 0 deletions packages/vite/vitest.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig, configDefaults } from 'vitest/config'

export default defineConfig({
test: {
exclude: [...configDefaults.exclude, '**/fixtures'],
},
})
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8862,12 +8862,12 @@ __metadata:
glob: "npm:10.3.10"
http-proxy-middleware: "npm:2.0.6"
isbot: "npm:3.7.1"
jest: "npm:29.7.0"
react: "npm:0.0.0-experimental-e5205658f-20230913"
react-server-dom-webpack: "npm:0.0.0-experimental-e5205658f-20230913"
rollup: "npm:3.29.4"
typescript: "npm:5.3.3"
vite: "npm:4.5.1"
vitest: "npm:1.2.1"
yargs-parser: "npm:21.1.1"
bin:
rw-dev-fe: ./dist/devFeServer.js
Expand Down

0 comments on commit 1a1e758

Please sign in to comment.