-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: move snapshot implementation into @vitest/snapshot (#3032)
- Loading branch information
1 parent
446308d
commit 6aff017
Showing
49 changed files
with
1,197 additions
and
1,082 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# @vitest/snapshot | ||
|
||
Lightweight implementation of Jest's snapshots. | ||
|
||
## Usage | ||
|
||
```js | ||
import { SnapshotClient } from '@vitest/snapshot' | ||
import { NodeSnapshotEnvironment } from '@vitest/snapshot/environment' | ||
import { SnapshotManager } from '@vitest/snapshot/manager' | ||
|
||
export class CustomSnapshotClient extends SnapshotClient { | ||
// by default, @vitest/snapshot checks equality with `!==` | ||
// you need to provide your own equality check implementation | ||
// this function is called when `.toMatchSnapshot({ property: 1 })` is called | ||
equalityCheck(received, expected) { | ||
return equals(received, expected, [iterableEquality, subsetEquality]) | ||
} | ||
} | ||
|
||
const client = new CustomSnapshotClient() | ||
// class that implements snapshot saving and reading | ||
// by default uses fs module, but you can provide your own implementation depending on the environment | ||
const environment = new NodeSnapshotEnvironment() | ||
|
||
const getCurrentFilepath = () => '/file.spec.ts' | ||
const getCurrentTestName = () => 'test1' | ||
|
||
const wrapper = (received) => { | ||
function __INLINE_SNAPSHOT__(inlineSnapshot, message) { | ||
client.assert({ | ||
received, | ||
message, | ||
isInline: true, | ||
inlineSnapshot, | ||
// you need to implement this yourselves, | ||
// this depends on your runner | ||
filepath: getCurrentFilepath(), | ||
name: getCurrentTestName(), | ||
}) | ||
} | ||
return { | ||
// the name is hard-coded, it should be inside another function, so Vitest can find the actual test file where it was called (parses call stack trace + 2) | ||
// you can override this behaviour in SnapshotState's `_inferInlineSnapshotStack` method by providing your own SnapshotState to SnapshotClient constructor | ||
toMatchInlineSnapshot: (...args) => __INLINE_SNAPSHOT__(...args), | ||
} | ||
} | ||
|
||
const options = { | ||
updateSnapshot: 'new', | ||
snapshotEnvironment: environment, | ||
} | ||
|
||
await client.setTest(getCurrentFilepath(), getCurrentTestName(), options) | ||
|
||
// uses "pretty-format", so it requires quotes | ||
// also naming is hard-coded when parsing test files | ||
wrapper('text 1').toMatchInlineSnapshot() | ||
wrapper('text 2').toMatchInlineSnapshot('"text 2"') | ||
|
||
const result = await client.resetCurrent() // this saves files and returns SnapshotResult | ||
|
||
// you can use manager to manage several clients | ||
const manager = new SnapshotManager(options) | ||
manager.add(result) | ||
|
||
// do something | ||
// and then read the summary | ||
|
||
console.log(manager.summary) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
{ | ||
"name": "@vitest/snapshot", | ||
"type": "module", | ||
"version": "0.29.3", | ||
"description": "Vitest Snapshot Resolver", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/vitest-dev/vitest.git", | ||
"directory": "packages/snapshot" | ||
}, | ||
"sideEffects": false, | ||
"exports": { | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"import": "./dist/index.js" | ||
}, | ||
"./environment": { | ||
"types": "./dist/environment.d.ts", | ||
"import": "./dist/environment.js" | ||
}, | ||
"./manager": { | ||
"types": "./dist/manager.d.ts", | ||
"import": "./dist/manager.js" | ||
}, | ||
"./*": "./*" | ||
}, | ||
"main": "./dist/index.js", | ||
"module": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"files": [ | ||
"dist", | ||
"*.d.ts" | ||
], | ||
"scripts": { | ||
"build": "rimraf dist && rollup -c", | ||
"dev": "rollup -c --watch", | ||
"prepublishOnly": "pnpm build" | ||
}, | ||
"dependencies": { | ||
"magic-string": "^0.27.0", | ||
"pathe": "^1.1.0", | ||
"pretty-format": "^27.5.1" | ||
}, | ||
"devDependencies": { | ||
"@types/natural-compare": "^1.4.1", | ||
"@vitest/utils": "workspace:*", | ||
"natural-compare": "^1.4.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { builtinModules } from 'module' | ||
import esbuild from 'rollup-plugin-esbuild' | ||
import nodeResolve from '@rollup/plugin-node-resolve' | ||
import dts from 'rollup-plugin-dts' | ||
import commonjs from '@rollup/plugin-commonjs' | ||
import { defineConfig } from 'rollup' | ||
import pkg from './package.json' | ||
|
||
const external = [ | ||
...builtinModules, | ||
...Object.keys(pkg.dependencies || {}), | ||
...Object.keys(pkg.peerDependencies || {}), | ||
] | ||
|
||
const entries = { | ||
index: 'src/index.ts', | ||
environment: 'src/environment.ts', | ||
manager: 'src/manager.ts', | ||
} | ||
|
||
const plugins = [ | ||
nodeResolve({ | ||
preferBuiltins: true, | ||
}), | ||
commonjs(), | ||
esbuild({ | ||
target: 'node14', | ||
}), | ||
] | ||
|
||
export default defineConfig([ | ||
{ | ||
input: entries, | ||
output: { | ||
dir: 'dist', | ||
format: 'esm', | ||
entryFileNames: '[name].js', | ||
chunkFileNames: 'chunk-[name].js', | ||
}, | ||
external, | ||
plugins, | ||
onwarn, | ||
}, | ||
{ | ||
input: entries, | ||
output: { | ||
dir: 'dist', | ||
entryFileNames: '[name].d.ts', | ||
format: 'esm', | ||
}, | ||
external, | ||
plugins: [ | ||
dts({ respectExternal: true }), | ||
], | ||
onwarn, | ||
}, | ||
]) | ||
|
||
function onwarn(message) { | ||
if (['EMPTY_BUNDLE', 'CIRCULAR_DEPENDENCY'].includes(message.code)) | ||
return | ||
console.error(message) | ||
} |
Oops, something went wrong.