Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove depdency on config extension in runner, use provide/inject, add tests #14

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ name: Checks
on:
pull_request:
branches:
- develop
- main
workflow_dispatch:

jobs:
build:
name: 'Build & Release'
name: 'Run tests'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
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/*
84 changes: 84 additions & 0 deletions __test__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { beforeAll, describe, expect, it } from 'vitest';
import { InlineConfig, startVitest } from 'vitest/node';
import vCache from '../src';
import fs from 'node:fs/promises';
import { dirname, resolve } from 'node:path';

const isCached = async (path: string) => {
try {
await fs.stat(resolve(path));
return fs.readdir(resolve(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 writeFileRecursive = async (path: string, data: string) => {
await fs.mkdir(dirname(path), { recursive: true });
await fs.writeFile(path, data);
};

const run = async (config?: InlineConfig) => startVitest('test', undefined, {
watch: false,
}, {
plugins: [vCache({
dir: '__test__/.cache',
silent: true,
})],
test: {
include: ['__test__/tests/*.mock.ts'],
reporters: [{}],
...config,
},
}).then((vitest) => vitest.close().then(() => vitest));

describe('v-cache', () => {
beforeAll(async () => {
if (await fs.stat(resolve('__test__/.cache')).catch(() => null)) {
await fs.rm(resolve('__test__/.cache'), { recursive: true });
}
});

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 writeFileRecursive(resolve(`__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');
});
});
3 changes: 3 additions & 0 deletions __test__/tests/deep/deep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { nested } from './nested';

export const deep = `deep/${nested}`;
1 change: 1 addition & 0 deletions __test__/tests/deep/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './deep'
1 change: 1 addition & 0 deletions __test__/tests/deep/nested/dependency/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './variable'
5 changes: 5 additions & 0 deletions __test__/tests/deep/nested/dependency/variable.ts
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,
}),
);
1 change: 1 addition & 0 deletions __test__/tests/deep/nested/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './nested';
3 changes: 3 additions & 0 deletions __test__/tests/deep/nested/nested.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { variable } from './dependency';

export const nested = `nested/${variable}`
8 changes: 8 additions & 0 deletions __test__/tests/fail0.mock.ts
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);
});
});
11 changes: 11 additions & 0 deletions __test__/tests/fail1.mock.ts
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);
});
});
8 changes: 8 additions & 0 deletions __test__/tests/pass0.mock.ts
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);
});
});
8 changes: 8 additions & 0 deletions __test__/tests/pass1.mock.ts
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);
});
});
9 changes: 9 additions & 0 deletions __test__/tests/variable.mock.ts
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);
});
});
Loading