diff --git a/package.json b/package.json index d6f8f13..351f4eb 100644 --- a/package.json +++ b/package.json @@ -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": "tech@monito.com", "license": "MIT", diff --git a/runner/core/__mocks__/api.ts b/runner/core/__mocks__/api.ts index ee858f7..3c67870 100644 --- a/runner/core/__mocks__/api.ts +++ b/runner/core/__mocks__/api.ts @@ -52,46 +52,6 @@ export const client = { prTemplate: { text: '# Title', }, - jestConfig: { - text: `module.exports = ${JSON.stringify({ - transform: { - '.(ts|tsx|js)': 'ts-jest', - }, - roots: ['/src/', '/__tests__/'], - testEnvironment: 'node', - testResultsProcessor: '/../../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, - })}`, - }, }, }) ), diff --git a/runner/plugins/__tests__/plugin-jest.spec.ts b/runner/plugins/__tests__/plugin-jest.spec.ts index bbab3fa..805d38b 100644 --- a/runner/plugins/__tests__/plugin-jest.spec.ts +++ b/runner/plugins/__tests__/plugin-jest.spec.ts @@ -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: { @@ -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: ['/src/', '/__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) diff --git a/runner/plugins/plugin-jest.ts b/runner/plugins/plugin-jest.ts index 6f42b8b..9b45466 100644 --- a/runner/plugins/plugin-jest.ts +++ b/runner/plugins/plugin-jest.ts @@ -1,4 +1,5 @@ import { gql } from 'graphql-request' +import { parse } from 'json5' import { client } from '../core/api' import { PluginInput } from '../types' @@ -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 }