-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove depdency on config extension in runner, use provide/inject, ad…
…d tests
- Loading branch information
Showing
24 changed files
with
250 additions
and
823 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
.idea | ||
node_modules | ||
dist | ||
__test__/.cache | ||
.DS_Store | ||
__test__/tests/deep/nested/dependency/variables/* |
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,85 @@ | ||
import { beforeAll, describe, expect, it } from 'vitest'; | ||
import { InlineConfig, startVitest } from 'vitest/node'; | ||
import vCache from '../src'; | ||
import fs from 'node:fs/promises'; | ||
import { resolve } from 'node:path'; | ||
|
||
const isCached = async (path: string) => { | ||
try { | ||
await fs.stat(path); | ||
return fs.readdir(path).then((files) => Promise.all(files.map((file) => fs.readFile(resolve(path, file), 'utf-8').then(JSON.parse).then(({ data }) => !!data))).then((r) => r.some(Boolean))); | ||
} catch (e) { | ||
return false; | ||
} | ||
}; | ||
|
||
const run = async (config?: InlineConfig) => startVitest('test', undefined, { | ||
watch: false, | ||
}, { | ||
plugins: [vCache({ | ||
dir: '__test__/.cache', | ||
})], | ||
test: { | ||
include: ['__test__/tests/*.mock.ts'], | ||
reporters: [{}], | ||
...config, | ||
}, | ||
}).then((vitest) => vitest.close().then(() => vitest)); | ||
|
||
describe('v-cache', () => { | ||
beforeAll(async () => { | ||
if (await fs.stat('__test__/.cache').catch(() => null)) { | ||
await fs.rm('__test__/.cache', { recursive: true }); | ||
} | ||
await fs.readdir('__test__/tests/deep/nested/dependency/variables').then( | ||
(files) => Promise.all( | ||
files.map( | ||
(file) => fs.rm(resolve('__test__/tests/deep/nested/dependency/variables', file)), | ||
), | ||
), | ||
); | ||
}); | ||
|
||
it('should cache passing tests', { | ||
timeout: 10000, | ||
}, async () => { | ||
await run(); | ||
|
||
expect(await isCached('__test__/.cache/pass0.mock.ts')).toBe(true); | ||
expect(await isCached('__test__/.cache/pass1.mock.ts')).toBe(true); | ||
expect(await isCached('__test__/.cache/variable.mock.ts')).toBe(true); | ||
expect(await isCached('__test__/.cache/fail0.mock.ts')).toBe(false); | ||
expect(await isCached('__test__/.cache/fail1.mock.ts')).toBe(false); | ||
}); | ||
|
||
it('should restore cached tests from cache', async () => { | ||
const vitest = await run(); | ||
|
||
const files = [...vitest.state.filesMap.values()].flatMap((files) => files); | ||
|
||
for (const file of files) { | ||
if (file.result.state === 'pass') { | ||
expect(file).toHaveProperty('cache', true); | ||
} else { | ||
expect(file).not.toHaveProperty('cache'); | ||
} | ||
} | ||
}); | ||
|
||
it('should rerun only the tests affected by change', async () => { | ||
await run(); | ||
|
||
const timestamp = Date.now(); | ||
await fs.writeFile(`__test__/tests/deep/nested/dependency/variables/${timestamp}.ts`, `export default '${timestamp}';`); | ||
|
||
const vitest = await run(); | ||
|
||
const files = Object.fromEntries([...vitest.state.filesMap.values()].flatMap((files) => files).map((file) => [file.filepath.match(/__test__.*$/)?.[0], file])); | ||
|
||
expect(files['__test__/tests/variable.mock.ts']).not.toHaveProperty('cache'); | ||
expect(files['__test__/tests/pass0.mock.ts']).toHaveProperty('cache', true); | ||
expect(files['__test__/tests/pass1.mock.ts']).toHaveProperty('cache', true); | ||
expect(files['__test__/tests/fail0.mock.ts']).not.toHaveProperty('cache'); | ||
expect(files['__test__/tests/fail1.mock.ts']).not.toHaveProperty('cache'); | ||
}); | ||
}); |
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,3 @@ | ||
import { nested } from './nested'; | ||
|
||
export const deep = `deep/${nested}`; |
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 @@ | ||
export * from './deep' |
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 @@ | ||
export * from './variable' |
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,5 @@ | ||
export const variable = JSON.stringify( | ||
import.meta.glob<{ default: string }>('./variables/*.ts', { | ||
eager: true, | ||
}), | ||
); |
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 @@ | ||
export * from './nested'; |
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,3 @@ | ||
import { variable } from './dependency'; | ||
|
||
export const nested = `nested/${variable}` |
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,8 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
|
||
describe('fails 0', () => { | ||
it('should fail', async () => { | ||
expect(true).toBe(false); | ||
}); | ||
}); |
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,11 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
|
||
describe('fails 1', () => { | ||
it('should pass', async () => { | ||
expect(true).toBe(true); | ||
}); | ||
it('should fail', async () => { | ||
expect(true).toBe(false); | ||
}); | ||
}); |
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,8 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
|
||
describe('passes 0', () => { | ||
it('should pass', async () => { | ||
expect(true).toBe(true); | ||
}); | ||
}); |
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,8 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
|
||
describe('passes 1', () => { | ||
it('should pass', async () => { | ||
expect(true).toBe(true); | ||
}); | ||
}); |
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,9 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { deep } from './deep'; | ||
|
||
|
||
describe('passes 2', () => { | ||
it('should pass', async () => { | ||
expect(deep).toBe(deep); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,19 +1,30 @@ | ||
import path from 'node:path'; | ||
import { OutputAsset, PluginContext, RollupOutput } from 'rollup'; | ||
import { OutputAsset, RollupOutput } from 'rollup'; | ||
import { build } from 'vite'; | ||
import type { ResolvedConfig } from 'vitest'; | ||
import { here } from './util.js'; | ||
import type { CacheEntry } from './cache.js'; | ||
|
||
export const load = (files: string[], config: ResolvedConfig) => { | ||
const dir = path.resolve(config.vCache.dir); | ||
const mapOutput = (output: RollupOutput['output']): { | ||
[k: string]: CacheEntry; | ||
} => | ||
Object.fromEntries( | ||
output | ||
.filter( | ||
(entry): entry is OutputAsset => entry.type === 'asset', | ||
) | ||
.map( | ||
({ | ||
name, | ||
source, | ||
}) => [name, JSON.parse(`${source}`)], | ||
), | ||
); | ||
|
||
return build({ | ||
configFile: here('./tests.vite.config'), | ||
build: { | ||
outDir: config.vCache.dir, | ||
rollupOptions: { | ||
input: files, | ||
}, | ||
export const load = (files: string[], dir: string) => build({ | ||
configFile: here('./tests.vite.config'), | ||
build: { | ||
outDir: dir, | ||
rollupOptions: { | ||
input: files, | ||
}, | ||
}).then(({ output }: RollupOutput) => Object.fromEntries(output.filter((entry): entry is OutputAsset => entry.type === 'asset').map((cache) => [path.resolve(dir, path.dirname(cache.fileName)), JSON.parse(`${cache.source}`)]))); | ||
}; | ||
}, | ||
}).then(({ output }: RollupOutput) => mapOutput(output)) |
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,11 @@ | ||
import type { TaskState } from 'vitest'; | ||
import type { CacheStrategy } from './strategy.js'; | ||
|
||
export interface CacheOptions { | ||
/* location for the caches, relative to the project root */ | ||
dir?: string; // default: '.tests' | ||
/* default: ['pass'] - which task states (test outcomes) should be cached */ | ||
states?: TaskState[]; // default: ['pass'] by default only passing tests are cached, failing tests are always rerun | ||
silent?: boolean; // default: false - if true, vCache will not write anything to stdout | ||
strategy?: CacheStrategy; | ||
} |
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
Oops, something went wrong.