Skip to content

Commit

Permalink
Merge branch 'master' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
allohamora committed Jan 30, 2025
2 parents 39cf4fd + dbba47d commit b263a06
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 104 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ jobs:
- name: Collect coverage
run: npm run test:coverage
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
uses: codecov/codecov-action@v5
59 changes: 21 additions & 38 deletions __tests__/categories/js/eslint/eslint.entrypoint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as fs from 'src/utils/fs';
import * as npm from 'src/utils/npm';
import * as mutation from 'src/utils/mutation';
import * as config from 'src/categories/js/eslint/eslint.config';
import * as javascript from 'src/utils/javascript';
import { eslint } from 'src/categories/js/eslint/eslint.entrypoint';
import { createConfig } from './eslint-test.utils';
import { Config } from 'src/categories/js/eslint/config/config.interface';
Expand All @@ -18,6 +19,14 @@ const mutationMocked = jest.mocked(mutation);
jest.mock('src/categories/js/eslint/eslint.config');
const configMocked = jest.mocked(config);

// we need to mock prettier because it doesn't work with jest
// TypeError: A dynamic import callback was invoked without --experimental-vm-modules
// https://github.com/prettier/prettier/issues/15769
jest.mock('src/utils/javascript', () => ({
format: jest.fn().mockImplementation(async (config) => config),
}));
const javascriptMocked = jest.mocked(javascript);

beforeEach(() => {
jest.clearAllMocks();
});
Expand Down Expand Up @@ -57,15 +66,12 @@ describe('eslint', () => {
configMocked.getConfig.mockReturnValueOnce(config);

const configFile = `export default [
{
ignores: [
'__test__'
],
}
{ignores: ["__test__"]},
];`;

await eslint();

expect(javascriptMocked.format).toHaveBeenCalledWith(configFile);
expect(fsMocked.addFileToRoot).toHaveBeenCalledWith('eslint.config.mjs', configFile);
});

Expand All @@ -74,12 +80,11 @@ describe('eslint', () => {
configMocked.getConfig.mockReturnValueOnce(config);

const configFile = `export default [
{\n
}
];`;

await eslint();

expect(javascriptMocked.format).toHaveBeenCalledWith(configFile);
expect(fsMocked.addFileToRoot).toHaveBeenCalledWith('eslint.config.mjs', configFile);
});

Expand All @@ -94,16 +99,12 @@ describe('eslint', () => {
configMocked.getConfig.mockReturnValueOnce(config);

const configFile = `export default [
{
languageOptions: {
globals: {\n
}
},
}
{languageOptions: {}}
];`;

await eslint();

expect(javascriptMocked.format).toHaveBeenCalledWith(configFile);
expect(fsMocked.addFileToRoot).toHaveBeenCalledWith('eslint.config.mjs', configFile);
});

Expand All @@ -114,14 +115,13 @@ describe('eslint', () => {
configMocked.getConfig.mockReturnValueOnce(config);

const configFile = `export default [
__test1__,
__test2__,
{\n
}
__test1__,
__test2__,
];`;

await eslint();

expect(javascriptMocked.format).toHaveBeenCalledWith(configFile);
expect(fsMocked.addFileToRoot).toHaveBeenCalledWith('eslint.config.mjs', configFile);
});

Expand Down Expand Up @@ -149,32 +149,15 @@ describe('eslint', () => {
configMocked.getConfig.mockReturnValueOnce(config);

const configFile = `// @ts-check
import globals from "globals";\n
import globals from "globals";
export default tseslint.config(
{
files: ['src/**/*.ts'],
ignores: [
'node_modules'
],
languageOptions: {
globals: {
...globals.window
},
parserOptions: {
ecmaVersion: 2020
}
},
plugins: {
'@typescript-eslint': eslintPluginTs
},
rules: {
'no-console': 'warn'
},
}
{ignores: ["node_modules"]},
{files: ["src/**/*.ts"],languageOptions: {globals: {...globals.window},parserOptions: {"ecmaVersion":2020}},plugins: {'@typescript-eslint': eslintPluginTs},rules: {"no-console":"warn"}}
);`;

await eslint();

expect(javascriptMocked.format).toHaveBeenCalledWith(configFile);
expect(fsMocked.addFileToRoot).toHaveBeenCalledWith('eslint.config.mjs', configFile);
});

Expand Down
4 changes: 4 additions & 0 deletions __tests__/setup-after-env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// we need to mock prettier in all tests because it doesn't work with jest
// TypeError: A dynamic import callback was invoked without --experimental-vm-modules
// https://github.com/prettier/prettier/issues/15769
jest.mock('prettier', () => jest.fn());
31 changes: 31 additions & 0 deletions __tests__/utils/javascript.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import prettier from 'prettier';
import { getConfig } from 'src/categories/js/prettier/prettier.config';
import { defaultConfig } from 'src/categories/js/prettier/config/default.config';
import { format } from 'src/utils/javascript';

// we need to mock prettier because it doesn't work with jest
// TypeError: A dynamic import callback was invoked without --experimental-vm-modules
// https://github.com/prettier/prettier/issues/15769
jest.mock('prettier', () => ({
format: jest.fn(),
}));
const prettierMocked = jest.mocked(prettier);

jest.mock('src/categories/js/prettier/prettier.config', () => ({ getConfig: jest.fn() }));
const getConfigMocked = jest.mocked(getConfig);

describe('format', () => {
test('formats input with default config', async () => {
const input = `const foo = "bar"`;
const formatted = `const foo = 'bar';`;

getConfigMocked.mockImplementation(() => ({ config: { printWidth: 90 } }) as typeof defaultConfig);
prettierMocked.format.mockResolvedValueOnce(formatted);

const actual = await format(input);
const expected = formatted;

expect(actual).toBe(expected);
expect(prettierMocked.format).toHaveBeenCalledWith(input, { parser: 'typescript', printWidth: 90 });
});
});
3 changes: 2 additions & 1 deletion jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
moduleDirectories: ['<rootDir>', 'node_modules'],
testRegex: '.*\.(spec|test)\.ts$',
testRegex: '.*.(spec|test).ts$',
collectCoverageFrom: ['src/**/*.ts'],
passWithNoTests: true,
setupFilesAfterEnv: ['./__tests__/setup-after-env.ts'],
};
70 changes: 53 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
"husky": "^9.1.6",
"jest": "^29.7.0",
"lint-staged": "^15.2.10",
"prettier": "^3.3.3",
"rimraf": "^6.0.1",
"rollup": "^3.29.5",
"rollup-plugin-typescript2": "^0.36.0",
Expand All @@ -83,7 +82,8 @@
},
"dependencies": {
"inquirer": "8.2.4",
"ora": "5.4.1"
"ora": "5.4.1",
"prettier": "^3.3.3"
},
"lint-staged": {
"*.ts": [
Expand Down
Loading

0 comments on commit b263a06

Please sign in to comment.