Skip to content

Commit

Permalink
fix(config): support async function configuration
Browse files Browse the repository at this point in the history
Closes #10
  • Loading branch information
rbardini committed Apr 24, 2022
1 parent 65f9437 commit c06f11a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 28 deletions.
78 changes: 53 additions & 25 deletions lib/__tests__/getData.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ const fs = require('fs')
const getData = require('../getData')

const configPath = './jest.config.js'

const coverageThreshold = {
global: {
branches: 10,
functions: 20,
lines: 30,
statements: 40,
},
}
const reportContents = JSON.stringify({
total: {
branches: { pct: 80 },
Expand All @@ -16,27 +23,43 @@ const reportContents = JSON.stringify({
jest.mock('fs')
jest.spyOn(process, 'cwd').mockImplementation(() => '/workingDir')

jest.mock(
'../jest.config.js',
() => ({
coverageThreshold: {
global: {
branches: 10,
functions: 20,
lines: 30,
statements: 40,
},
},
}),
{ virtual: true },
)
beforeAll(() => fs.readFileSync.mockReturnValue(reportContents))
beforeEach(() => jest.resetModules())

it('returns parsed config and report contents', async () => {
jest.mock('../jest.config.js', () => ({ coverageThreshold }), {
virtual: true,
})

const data = await getData(configPath)

beforeAll(() => {
fs.readFileSync.mockReturnValue(reportContents)
expect(fs.readFileSync).toHaveBeenCalledTimes(1)
expect(fs.readFileSync).toHaveBeenCalledWith(
'/workingDir/coverage/coverage-summary.json',
'utf8',
)
expect(data).toStrictEqual({
coverages: {
branches: { pct: 80 },
functions: { pct: 70 },
lines: { pct: 60 },
statements: { pct: 50 },
},
thresholds: {
branches: 10,
functions: 20,
lines: 30,
statements: 40,
},
})
})

it('returns parsed config and report contents when no coverage directory is specified in the jest config', () => {
const data = getData(configPath)
it('returns parsed async config and report contents', async () => {
jest.mock('../jest.config.js', () => async () => ({ coverageThreshold }), {
virtual: true,
})

const data = await getData(configPath)

expect(fs.readFileSync).toHaveBeenCalledTimes(1)
expect(fs.readFileSync).toHaveBeenCalledWith(
Expand All @@ -59,11 +82,17 @@ it('returns parsed config and report contents when no coverage directory is spec
})
})

it('returns parsed config and report contents when a custom coverage directory is specified in the jest config', () => {
const config = require('../jest.config.js')
const { coverageDirectory: defaultCovergeDirectory } = config
config.coverageDirectory = 'custom/coverage/directory'
const data = getData(configPath)
it('returns parsed config and report contents when a custom coverage directory is specified in the jest config', async () => {
jest.mock(
'../jest.config.js',
() => ({
coverageThreshold,
coverageDirectory: 'custom/coverage/directory',
}),
{ virtual: true },
)

const data = await getData(configPath)

expect(fs.readFileSync).toHaveBeenCalledTimes(1)
expect(fs.readFileSync).toHaveBeenCalledWith(
Expand All @@ -84,5 +113,4 @@ it('returns parsed config and report contents when a custom coverage directory i
statements: 40,
},
})
config.coverageDirectory = defaultCovergeDirectory
})
5 changes: 3 additions & 2 deletions lib/getData.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const fs = require('fs')
const path = require('path')

const getData = configPath => {
const getData = async configPath => {
const config = require(configPath)
const {
coverageThreshold: { global: thresholds },
coverageDirectory = 'coverage',
} = require(configPath)
} = typeof config === 'function' ? await config() : config
const reportPath = path.resolve(
process.cwd(),
coverageDirectory,
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = async ({
} = {}) => {
const configPath = path.resolve(process.cwd(), 'jest.config.js')

const { thresholds, coverages } = getData(configPath)
const { thresholds, coverages } = await getData(configPath)
const newThresholds = getNewThresholds(thresholds, coverages, margin)
const { changes, data } = getChanges(configPath, newThresholds)

Expand Down

0 comments on commit c06f11a

Please sign in to comment.