Skip to content

Commit

Permalink
feat: jsdom
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Dec 5, 2021
1 parent 516136d commit ac4df04
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 6 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ A blazing fast test runner powered by Vite.

## Features

- [Vite](https://vitejs.dev/)'s config, transformers, resolvers, and plugins. Powered by [vite-node](https://github.com/antfu/vite-node)
- [Vite](https://vitejs.dev/)'s config, transformers, resolvers, and plugins.
- [Jest Snapshot](https://jestjs.io/docs/snapshot-testing)
- [Chai](https://www.chaijs.com/) for assertions
- [Sinon](https://sinonjs.org/) for mocking
- [JSDOM](https://github.com/jsdom/jsdom) for DOM mocking
- Async suite / test, top level await
- ESM friendly
- Out-of-box TypeScript support
Expand All @@ -20,11 +21,12 @@ import { it, describe, expect, assert } from 'vitest'

describe('suite name', () => {
it('foo', () => {
assert.equal(Math.sqrt(4), 2)
expect(1 + 1).toEqual(2)
expect(true).to.be.true
})

it('bar', () => {
expect(1 + 1).eq(2)
assert.equal(Math.sqrt(4), 2)
})

it('snapshot', () => {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"devDependencies": {
"@antfu/eslint-config": "^0.11.1",
"@antfu/ni": "^0.11.0",
"@types/jsdom": "^16.2.13",
"@types/minimist": "^1.2.2",
"@types/node": "^16.11.11",
"@types/sinon": "^10.0.6",
Expand Down
18 changes: 18 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const argv = minimist(process.argv.slice(2), {
w: 'watch',
},
string: ['root', 'config'],
boolean: ['update', 'dev', 'global', 'watch'],
boolean: ['update', 'dev', 'global', 'watch', 'jsdom'],
unknown(name) {
if (name[0] === '-') {
console.error(c.red(`Unknown argument: ${name}`))
Expand All @@ -28,8 +28,7 @@ const viteConfig = server?.config || {}
const testOptions = viteConfig.test || {}

await run({
global: argv.global,
watch: argv.watch,
...argv,
...testOptions,
server,
updateSnapshot: argv.update,
Expand Down
26 changes: 26 additions & 0 deletions src/integrations/jsdom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { JSDOM } from 'jsdom'

export function setupJSDOM(global: any) {
const dom = new JSDOM('<!DOCTYPE html>',
{
pretendToBeVisual: true,
runScripts: 'dangerously',
// TODO: options
url: 'http://localhost:3000',
},
)

const keys = Object.getOwnPropertyNames(dom.window)
.filter(k => !k.startsWith('_'))
.filter(k => !(k in global))

for (const key of keys)
global[key] = dom.window[key]

return {
dom,
restore() {
keys.forEach(key => delete global[key])
},
}
}
3 changes: 3 additions & 0 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ export async function run(config: Config) {
if (config.global)
(await import('./global')).registerApiGlobally()

if (config.jsdom)
(await import('./integrations/jsdom')).setupJSDOM(globalThis)

const files = await collectFiles(paths)

const ctx: RunnerContext = {
Expand Down

0 comments on commit ac4df04

Please sign in to comment.