Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6e07d44
commit ae02421
Showing
6 changed files
with
189 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { createHash } from 'crypto' | ||
import { resolve } from 'pathe' | ||
import { slash } from 'vite-node/utils' | ||
import type { Vitest } from '../core' | ||
import type { TestSequelizer } from './types' | ||
|
||
export class BaseSequelizer implements TestSequelizer { | ||
protected ctx: Vitest | ||
|
||
constructor(ctx: Vitest) { | ||
this.ctx = ctx | ||
} | ||
|
||
// async so it can be extended by other sequelizers | ||
public async shard(files: string[]): Promise<string[]> { | ||
const config = this.ctx.getSerializableConfig() | ||
const { index, count } = config.shard! | ||
const shardSize = Math.ceil(files.length / count) | ||
const shardStart = shardSize * (index - 1) | ||
const shardEnd = shardSize * index | ||
return [...files] | ||
.map((file) => { | ||
const fullPath = resolve(slash(config.root), slash(file)) | ||
const specPath = fullPath.slice(config.root.length) | ||
return { | ||
file, | ||
hash: createHash('sha1') | ||
.update(specPath) | ||
.digest('hex'), | ||
} | ||
}) | ||
.sort((a, b) => (a.hash < b.hash ? -1 : a.hash > b.hash ? 1 : 0)) | ||
.slice(shardStart, shardEnd) | ||
.map(({ file }) => file) | ||
} | ||
|
||
// async so it can be extended by other sequelizers | ||
public async sort(files: string[]): Promise<string[]> { | ||
const ctx = this.ctx | ||
return [...files].sort((a, b) => { | ||
const aState = ctx.state.getFileTestResults(a) | ||
const bState = ctx.state.getFileTestResults(b) | ||
|
||
if (!aState || !bState) { | ||
const statsA = ctx.state.getFileStats(a) | ||
const statsB = ctx.state.getFileStats(b) | ||
|
||
// run unknown forst | ||
if (!statsA || !statsB) | ||
return !statsA && statsB ? -1 : !statsB && statsA ? 1 : 0 | ||
|
||
// run larger files first | ||
return statsB.size - statsA.size | ||
} | ||
|
||
// run failed first | ||
if (aState.failed && !bState.failed) | ||
return -1 | ||
if (!aState.failed && bState.failed) | ||
return 1 | ||
|
||
// run longer first | ||
return bState.duration - aState.duration | ||
}) | ||
} | ||
} |
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,10 @@ | ||
import type { Awaitable } from '../../types' | ||
|
||
export interface TestSequelizer { | ||
/** | ||
* Slicing tests into shards. Will be run before `sort`. | ||
* Only run, if `shard` is defined. | ||
*/ | ||
shard(files: string[]): Awaitable<string[]> | ||
sort(files: string[]): Awaitable<string[]> | ||
} |
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,97 @@ | ||
import type { Vitest } from 'vitest' | ||
import { describe, expect, test, vi } from 'vitest' | ||
import { BaseSequelizer } from '../../../packages/vitest/src/node/sequelizers/BaseSequelizer' | ||
|
||
const buildCtx = () => { | ||
return { | ||
state: { | ||
getFileTestResults: vi.fn(), | ||
getFileStats: vi.fn(), | ||
}, | ||
} as unknown as Vitest | ||
} | ||
|
||
describe('test sequelizers', () => { | ||
test('sorting when no info is available', async () => { | ||
const sequelizer = new BaseSequelizer(buildCtx()) | ||
const files = ['a', 'b', 'c'] | ||
const sorted = await sequelizer.sort(files) | ||
expect(sorted).toStrictEqual(files) | ||
}) | ||
|
||
test('prioritaze unknown files', async () => { | ||
const ctx = buildCtx() | ||
vi.spyOn(ctx.state, 'getFileStats').mockImplementation((file) => { | ||
if (file === 'b') | ||
return { size: 2 } | ||
}) | ||
const sequelizer = new BaseSequelizer(ctx) | ||
const files = ['b', 'a', 'c'] | ||
const sorted = await sequelizer.sort(files) | ||
expect(sorted).toStrictEqual(['a', 'c', 'b']) | ||
}) | ||
|
||
test('sort by size, larger first', async () => { | ||
const ctx = buildCtx() | ||
vi.spyOn(ctx.state, 'getFileStats').mockImplementation((file) => { | ||
if (file === 'a') | ||
return { size: 1 } | ||
if (file === 'b') | ||
return { size: 2 } | ||
if (file === 'c') | ||
return { size: 3 } | ||
}) | ||
const sequelizer = new BaseSequelizer(ctx) | ||
const files = ['b', 'a', 'c'] | ||
const sorted = await sequelizer.sort(files) | ||
expect(sorted).toStrictEqual(['c', 'b', 'a']) | ||
}) | ||
|
||
test('sort by results, failed first', async () => { | ||
const ctx = buildCtx() | ||
vi.spyOn(ctx.state, 'getFileTestResults').mockImplementation((file) => { | ||
if (file === 'a') | ||
return { failed: false, duration: 1 } | ||
if (file === 'b') | ||
return { failed: true, duration: 1 } | ||
if (file === 'c') | ||
return { failed: true, duration: 1 } | ||
}) | ||
const sequelizer = new BaseSequelizer(ctx) | ||
const files = ['b', 'a', 'c'] | ||
const sorted = await sequelizer.sort(files) | ||
expect(sorted).toStrictEqual(['b', 'c', 'a']) | ||
}) | ||
|
||
test('sort by results, long first', async () => { | ||
const ctx = buildCtx() | ||
vi.spyOn(ctx.state, 'getFileTestResults').mockImplementation((file) => { | ||
if (file === 'a') | ||
return { failed: true, duration: 1 } | ||
if (file === 'b') | ||
return { failed: true, duration: 2 } | ||
if (file === 'c') | ||
return { failed: true, duration: 3 } | ||
}) | ||
const sequelizer = new BaseSequelizer(ctx) | ||
const files = ['b', 'a', 'c'] | ||
const sorted = await sequelizer.sort(files) | ||
expect(sorted).toStrictEqual(['c', 'b', 'a']) | ||
}) | ||
|
||
test('sort by results, long and failed first', async () => { | ||
const ctx = buildCtx() | ||
vi.spyOn(ctx.state, 'getFileTestResults').mockImplementation((file) => { | ||
if (file === 'a') | ||
return { failed: false, duration: 1 } | ||
if (file === 'b') | ||
return { failed: false, duration: 6 } | ||
if (file === 'c') | ||
return { failed: true, duration: 3 } | ||
}) | ||
const sequelizer = new BaseSequelizer(ctx) | ||
const files = ['b', 'a', 'c'] | ||
const sorted = await sequelizer.sort(files) | ||
expect(sorted).toStrictEqual(['c', 'b', 'a']) | ||
}) | ||
}) |