-
Notifications
You must be signed in to change notification settings - Fork 340
/
affected-files.test.mjs
74 lines (55 loc) · 2.71 KB
/
affected-files.test.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { join } from 'path'
import test from 'ava'
import glob from 'fast-glob'
import mock from 'mock-fs'
import { stub, createSandbox } from 'sinon'
import { simpleMockedFileSystem } from './utils/file-systems.mjs'
/**
* Get a list of affected files for a mocked file system
* @param {string[]} changedFiles The list of changed files
* @param {Record<string, string>} fileSystem The mocked file system
* @returns Returns a list of affected files
*/
const getAffectedFilesFromMock = async (changedFiles, fileSystem = simpleMockedFileSystem) => {
const mockedTestFiles = Object.keys(fileSystem).filter((file) => file.match(/\.test\.m?js$/gm))
const globStub = stub(glob, 'sync').returns(mockedTestFiles)
const { getAffectedFiles } = await import('../affected-test.mjs')
mock(fileSystem)
const affectedFiles = getAffectedFiles(changedFiles)
mock.restore()
globStub.restore()
return { affectedFiles, mockedTestFiles }
}
test.beforeEach((t) => {
t.context.sandbox = createSandbox()
})
test.afterEach((t) => {
t.context.sandbox.restore()
})
test.only('should get all files marked as affected when the package.json is touched', async (t) => {
const consoleStub = t.context.sandbox.stub(console, 'log').callsFake(() => {})
const { affectedFiles, mockedTestFiles } = await getAffectedFilesFromMock(['package.json'])
t.truthy(consoleStub.firstCall.calledWith('All files are affected based on the changeset'))
t.deepEqual(affectedFiles, mockedTestFiles)
})
test.serial('should get all files marked as affected when the npm-shrinkwrap.json is touched', async (t) => {
const consoleStub = t.context.sandbox.stub(console, 'log').callsFake(() => {})
const { affectedFiles, mockedTestFiles } = await getAffectedFilesFromMock(['npm-shrinkwrap.json'])
t.truthy(consoleStub.firstCall.calledWith('All files are affected based on the changeset'))
t.deepEqual(affectedFiles, mockedTestFiles)
})
test.serial('should get all files marked as affected when a leaf is touched that both tests depend on', async (t) => {
const consoleStub = stub(console, 'log').callsFake(() => {})
const { affectedFiles, mockedTestFiles } = await getAffectedFilesFromMock([join('src/d.js')])
t.truthy(consoleStub.notCalled)
t.deepEqual(affectedFiles, mockedTestFiles)
consoleStub.restore()
})
test.serial('should only one test affected if a file for it was called', async (t) => {
const { affectedFiles } = await getAffectedFilesFromMock([join('src/nested/b.js')])
t.deepEqual(affectedFiles, [join('tests/a.test.js')])
})
test.serial('should not have any file affected if a different file like a readme was affected', async (t) => {
const { affectedFiles } = await getAffectedFilesFromMock(['README.md'])
t.is(affectedFiles.length, 0)
})