Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into chore-repro-templates
Browse files Browse the repository at this point in the history
hi-ogawa committed Jan 20, 2024

Verified

This commit was signed with the committer’s verified signature. The key has expired.
hi-ogawa Hiroshi Ogawa
2 parents 78b3840 + d53b858 commit 2f60fa4
Showing 12 changed files with 375 additions and 133 deletions.
2 changes: 1 addition & 1 deletion docs/guide/ide.md
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ title: IDE Integrations | Guide

# IDE Integrations

## VS Code <Badge>Offical</Badge>
## VS Code <Badge>Official</Badge>

<p text-center>
<img src="https://raw.githubusercontent.com/vitest-dev/vscode/main/img/cover.png" w-60>
2 changes: 1 addition & 1 deletion docs/guide/testing-types.md
Original file line number Diff line number Diff line change
@@ -117,7 +117,7 @@ assertType<string>(answr) //

## Run Typechecking

Since Vitest 1.0, to enabled typechecking, just add [`--typecheck`](/config/#typecheck) flag to your Vitest command in `package.json`:
Since Vitest 1.0, to enable typechecking, just add [`--typecheck`](/config/#typecheck) flag to your Vitest command in `package.json`:

```json
{
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -43,7 +43,7 @@
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-json": "^6.0.1",
"@rollup/plugin-node-resolve": "^15.2.3",
"@types/node": "^18.18.9",
"@types/node": "^20.11.5",
"@types/ws": "^8.5.9",
"@vitest/browser": "workspace:*",
"@vitest/coverage-istanbul": "workspace:*",
6 changes: 3 additions & 3 deletions packages/coverage-v8/src/provider.ts
Original file line number Diff line number Diff line change
@@ -235,10 +235,10 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage
},
})
}

this.coverageFiles = new Map()
await fs.rm(this.coverageFilesDirectory, { recursive: true })
}

this.coverageFiles = new Map()
await fs.rm(this.coverageFilesDirectory, { recursive: true })
}

private async getUntestedFiles(testedFiles: string[]): Promise<RawCoverage> {
12 changes: 9 additions & 3 deletions packages/vite-node/src/client.ts
Original file line number Diff line number Diff line change
@@ -296,7 +296,14 @@ export class ViteNodeRunner {
const modulePath = cleanUrl(moduleId)
// disambiguate the `<UNIT>:/` on windows: see nodejs/node#31710
const href = pathToFileURL(modulePath).href
const meta = { url: href, env }
const __filename = fileURLToPath(href)
const __dirname = dirname(__filename)
const meta = {
url: href,
env,
filename: __filename,
dirname: __dirname,
}
const exports = Object.create(null)
Object.defineProperty(exports, Symbol.toStringTag, {
value: 'Module',
@@ -344,7 +351,6 @@ export class ViteNodeRunner {
})

Object.assign(mod, { code: transformed, exports })
const __filename = fileURLToPath(href)
const moduleProxy = {
set exports(value) {
exportAll(cjsExports, value)
@@ -388,7 +394,7 @@ export class ViteNodeRunner {
exports: cjsExports,
module: moduleProxy,
__filename,
__dirname: dirname(__filename),
__dirname,
})

debugExecute(__filename)
1 change: 1 addition & 0 deletions packages/vitest/package.json
Original file line number Diff line number Diff line change
@@ -173,6 +173,7 @@
"@types/istanbul-reports": "^3.0.4",
"@types/jsdom": "^21.1.6",
"@types/micromatch": "^4.0.6",
"@types/node": "^20.11.5",
"@types/prompts": "^2.4.9",
"@types/sinonjs__fake-timers": "^8.1.5",
"birpc": "0.2.14",
4 changes: 2 additions & 2 deletions packages/vitest/src/runtime/external-executor.ts
Original file line number Diff line number Diff line change
@@ -71,11 +71,11 @@ export class ExternalModulesExecutor {
}

public resolveModule = async (specifier: string, referencer: string) => {
const identifier = await this.resolve(specifier, referencer)
const identifier = this.resolve(specifier, referencer)
return await this.createModule(identifier)
}

public async resolve(specifier: string, parent: string) {
public resolve(specifier: string, parent: string) {
for (const resolver of this.resolvers) {
const id = resolver(specifier, parent)
if (id)
4 changes: 4 additions & 0 deletions packages/vitest/src/runtime/vm/commonjs-executor.ts
Original file line number Diff line number Diff line change
@@ -76,6 +76,10 @@ export class CommonjsExecutor {
return _require
}

static register = () => {
throw new Error(`[vitest] "register" is not available when running in Vitest.`)
}

_compile(code: string, filename: string) {
const cjsModule = Module.wrap(code)
const script = new vm.Script(cjsModule, {
11 changes: 9 additions & 2 deletions packages/vitest/src/runtime/vm/esm-executor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint-disable antfu/no-cjs-exports */

import type vm from 'node:vm'
import { fileURLToPath } from 'node:url'
import { dirname } from 'node:path'
import type { ExternalModulesExecutor } from '../external-executor'
import type { VMModule } from './types'
import { SourceTextModule, SyntheticModule } from './utils'
@@ -62,8 +64,13 @@ export class EsmExecutor {
importModuleDynamically: this.executor.importModuleDynamically,
initializeImportMeta: (meta, mod) => {
meta.url = mod.identifier
meta.resolve = (specifier: string, importer?: string) => {
return this.executor.resolve(specifier, importer ?? mod.identifier)
if (mod.identifier.startsWith('file:')) {
const filename = fileURLToPath(mod.identifier)
meta.filename = filename
meta.dirname = dirname(filename)
}
meta.resolve = (specifier: string, importer?: string | URL) => {
return this.executor.resolve(specifier, importer != null ? importer.toString() : mod.identifier)
}
},
},
449 changes: 329 additions & 120 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions test/core/test/file-path.test.ts
Original file line number Diff line number Diff line change
@@ -5,6 +5,14 @@ import { isWindows, slash, toFilePath } from '../../../packages/vite-node/src/ut
vi.mock('fs')

describe('current url', () => {
it('__filename is equal to import.meta.url', () => {
expect(__filename).toEqual(import.meta.filename)
})

it('__dirname is equal to import.meta.dirname', () => {
expect(__dirname).toEqual(import.meta.dirname)
})

describe.runIf(!isWindows)('unix', () => {
it('__filename', () => {
expect(__filename.startsWith('file://')).toBe(false)
Original file line number Diff line number Diff line change
@@ -184,3 +184,10 @@ test('multi environment coverage is merged correctly', async () => {
// Condition covered by both tests
expect(lineCoverage[30]).toBe(2)
})

test('temporary files are removed after test', async () => {
const coveragePath = resolve('./coverage')
const files = fs.readdirSync(coveragePath)

expect(files).not.toContain('.tmp')
})

0 comments on commit 2f60fa4

Please sign in to comment.