Skip to content

Commit

Permalink
feat: add jest config support (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
obertrand authored Jul 20, 2021
1 parent 47ad975 commit cb7fdff
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 66 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@monito/technical-stats",
"version": "1.0.0-beta.16",
"version": "1.0.0-beta.17",
"repository": "https://github.com/monito/technical-stats.git",
"author": "[email protected]",
"license": "MIT",
Expand Down
40 changes: 0 additions & 40 deletions runner/core/__mocks__/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,46 +52,6 @@ export const client = {
prTemplate: {
text: '# Title',
},
jestConfig: {
text: `module.exports = ${JSON.stringify({
transform: {
'.(ts|tsx|js)': 'ts-jest',
},
roots: ['<rootDir>/src/', '<rootDir>/__tests__/'],
testEnvironment: 'node',
testResultsProcessor: '<rootDir>/../../node_modules/jest-junit',
testRegex: '(/__tests__/.*\\.(test|spec))\\.(ts|tsx|js)$',
moduleFileExtensions: ['ts', 'tsx', 'js'],
modulePathIgnorePatterns: ['lib'],
coveragePathIgnorePatterns: ['/node_modules/', '/__tests__/'],
coverageThreshold: {
global: {
branches: 80,
functions: 85,
lines: 95,
statements: 95,
},
},
collectCoverage: true,
collectCoverageFrom: ['src/**.{js,ts}'],
reporters: [
'default',
[
'jest-junit',
{
suiteName: 'jest unit tests',
outputDirectory: '../../junit',
uniqueOutputName: 'true',
classNameTemplate: '{classname}',
titleTemplate: '{classname} {title}',
ancestorSeparator: ' › ',
usePathForSuiteName: 'true',
},
],
],
verbose: true,
})}`,
},
},
})
),
Expand Down
90 changes: 66 additions & 24 deletions runner/plugins/__tests__/plugin-jest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,12 @@ describe('jest', () => {
defaultBranchName: 'main',
})

it('return empty object if no jest config was found', async () => {
const request = client.request as unknown as jest.Mock
const notFoundCallback = () =>
Promise.resolve({
repository: {
jestConfig: undefined,
},
})
request
.mockImplementationOnce(notFoundCallback) // for jest.config.js
.mockImplementationOnce(notFoundCallback) // for jest.config.base.js
const output = await runPlugin()

expect(output).toEqual({})
})
const notFoundCallback = () =>
Promise.resolve({
repository: {
jestConfig: undefined,
},
})

const COVERAGE_THRESHOLD_MATCH = {
coverageThreshold: {
Expand All @@ -39,21 +30,72 @@ describe('jest', () => {
},
}

it('fetches jest config and parses it', async () => {
const jestConfigFile = () =>
Promise.resolve({
repository: {
jestConfig: {
text: `module.exports = {
transform: {
'.(ts|tsx|js)': 'ts-jest',
},
roots: ['<rootDir>/src/', '<rootDir>/__tests__/'],
testEnvironment: 'node',
testRegex: '(/__tests__/.*\\.(test|spec))\\.(ts|tsx|js)$',
moduleFileExtensions: ['ts', 'tsx', 'js'],
modulePathIgnorePatterns: ['lib'],
coveragePathIgnorePatterns: ['/node_modules/', '/__tests__/'],
coverageThreshold:${JSON.stringify(
COVERAGE_THRESHOLD_MATCH.coverageThreshold
)},
collectCoverage: true,
collectCoverageFrom: ['src/**.{js,ts}'],
reporters: [
'default',
[
'jest-junit',
{
suiteName: 'jest unit tests',
outputDirectory: 'junit',
outputName: 'user-id-test.xml',
classNameTemplate: '{classname}',
titleTemplate: '{classname} {title}',
ancestorSeparator: ' › ',
usePathForSuiteName: 'true',
},
],
],
verbose: true,
}
`,
},
},
})
it('return empty object if no jest config was found', async () => {
const request = client.request as unknown as jest.Mock
request
.mockImplementationOnce(notFoundCallback) // for jest.config.js
.mockImplementationOnce(notFoundCallback) // for jest.config.base.js

const output = await runPlugin()

expect(output).toEqual({})
})

it('fetches the base config if config not found', async () => {
const request = client.request as unknown as jest.Mock
request
.mockImplementationOnce(notFoundCallback) // for jest.config.js
.mockImplementationOnce(jestConfigFile)

const output = await runPlugin()

expect(output).toMatchObject(COVERAGE_THRESHOLD_MATCH)
})

it('fetches the base config is config not found', async () => {
it('fetches jest config and parses it', async () => {
const request = client.request as unknown as jest.Mock
const notFoundCallback = () =>
Promise.resolve({
repository: {
jestConfig: undefined,
},
})
request.mockImplementationOnce(notFoundCallback) // for jest.config.js
request.mockImplementationOnce(jestConfigFile)

const output = await runPlugin()

expect(output).toMatchObject(COVERAGE_THRESHOLD_MATCH)
Expand Down
3 changes: 2 additions & 1 deletion runner/plugins/plugin-jest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { gql } from 'graphql-request'
import { parse } from 'json5'
import { client } from '../core/api'
import { PluginInput } from '../types'

Expand All @@ -22,7 +23,7 @@ async function getJestConfig(project: PluginInput, fileName: string) {
} = await client.request(QUERY, { owner, name, configPath })

return jestConfig
? JSON.parse(jestConfig.text.substring('module.exports = '.length))
? parse(jestConfig.text.substring('module.exports = '.length))
: undefined
}

Expand Down

0 comments on commit cb7fdff

Please sign in to comment.