Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow use in workspaces #7

Merged
merged 3 commits into from Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 0 additions & 3 deletions packages/vite-jest/bin/vite-jest.js
Expand Up @@ -8,9 +8,6 @@ import { createRequire } from 'module'
const require = createRequire(import.meta.url)
const jestPath = require.resolve('jest/bin/jest')

const viteClientDirectory = path.join(process.cwd(), './node_modules/vite/dist/client')
fs.writeFileSync(path.join(viteClientDirectory, 'package.json'), JSON.stringify({ type: "module" }))

execa.sync('node', [
'--experimental-vm-modules',
jestPath,
Expand Down
7 changes: 5 additions & 2 deletions packages/vite-jest/index.js
Expand Up @@ -26,6 +26,9 @@ export function fsPathFromId(id) {

// TODO: use a createTransformer function to get rootDir
const rootDir = process.cwd()
// https://github.com/vitejs/vite/blob/v2.4.2/packages/vite/src/node/constants.ts#L44-L46
const CLIENT_ENTRY = require.resolve('vite/dist/client/client.mjs')
const ENV_ENTRY = require.resolve('vite/dist/client/env.mjs')

async function processAsync(src, filepath) {
const result = await viteServer.transformRequest(filepath)
Expand Down Expand Up @@ -56,9 +59,9 @@ async function processAsync(src, filepath) {
// FIXME: Temporary workaround.
// The root problem is that Jest can't resolve virtual files.
// So it may be better to create on-disk placeholder files for virtual files.
mStr.overwrite(start, end, require.resolve('vite/dist/client/env.js'))
mStr.overwrite(start, end, ENV_ENTRY)
} else if (url === '/@vite/client') {
mStr.overwrite(start, end, require.resolve('vite/dist/client/client.js'))
mStr.overwrite(start, end, CLIENT_ENTRY)
} else if (url.startsWith('/')) {
mStr.overwrite(start, end, path.join(rootDir, url))
}
Expand Down