Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Aug 3, 2023
1 parent b908b7d commit c79b7ba
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
11 changes: 10 additions & 1 deletion test/test-utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Console } from 'node:console'
import { Writable } from 'node:stream'
import fs from 'node:fs'
import { fileURLToPath } from 'node:url'
import { type UserConfig, type VitestRunMode, afterEach } from 'vitest'
import type { Vitest } from 'vitest/node'
import { startVitest } from 'vitest/node'
import { type Options, execa } from 'execa'
import stripAnsi from 'strip-ansi'
import { dirname, resolve } from 'pathe'

export async function runVitest(config: UserConfig, cliFilters: string[] = [], mode: VitestRunMode = 'test') {
// Reset possible previous runs
Expand Down Expand Up @@ -215,14 +217,16 @@ afterEach(() => {
fs.writeFileSync(file, content, 'utf-8')
})
createdFiles.forEach((file) => {
fs.unlinkSync(file)
if (fs.existsSync(file))
fs.unlinkSync(file)
})
originalFiles.clear()
createdFiles.clear()
})

export function createFile(file: string, content: string) {
createdFiles.add(file)
fs.mkdirSync(dirname(file), { recursive: true })
fs.writeFileSync(file, content, 'utf-8')
}

Expand All @@ -232,3 +236,8 @@ export function editFile(file: string, callback: (content: string) => string) {
originalFiles.set(file, content)
fs.writeFileSync(file, callback(content), 'utf-8')
}

export function resolvePath(baseUrl: string, path: string) {
const filename = fileURLToPath(baseUrl)
return resolve(dirname(filename), path)
}
1 change: 1 addition & 0 deletions test/vm-threads/src/external/package-null/package-null.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 42
4 changes: 2 additions & 2 deletions test/vm-threads/test/require-cjs.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { createRequire } from 'node:module'
import { describe, expect, test } from 'vitest'
import { createFile } from '../../test-utils'
import { createFile, resolvePath } from '../../test-utils'

const require = createRequire(import.meta.url)

test('can require if package.json is null', () => {
createFile('src/external/package-null/package-null.js', 'null')
createFile(resolvePath(import.meta.url, '../src/external/package-null/package-null.json'), 'null')
expect(() => require('../src/external/package-null/package-null.js')).not.toThrow()
})

Expand Down

0 comments on commit c79b7ba

Please sign in to comment.