Skip to content

Commit

Permalink
fix: [tester] collect coverage when test *
Browse files Browse the repository at this point in the history
  • Loading branch information
deot committed Aug 30, 2023
1 parent 89af6ab commit 064fe74
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 28 deletions.
10 changes: 8 additions & 2 deletions packages/shared/__tests__/locals.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as path from 'node:path';
import { Locals } from '@deot/dev-shared';

describe('shared.ts', () => {
Expand Down Expand Up @@ -42,7 +43,12 @@ describe('shared.ts', () => {
});

it('subpackage', () => {
expect(!!Locals.getSubpackages('index').length).toBe(false);
expect(!!Locals.getSubpackages('_/monorepo/packages/components').length).toBe(true);
const it$ = Locals.impl();
expect(!!it$.subpackagesMap['index'].length).toBe(false);
});

it('subpackage, components', () => {
const it$ = Locals.impl(path.resolve('./packages/_/monorepo'));
expect(!!it$.subpackagesMap['components'].length).toBe(true);
});
});
32 changes: 23 additions & 9 deletions packages/tester/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,47 @@ describe('index', () => {
});

it('monorepo', async () => {
expect.assertions(1);
const response = await Shell.spawn(`npm`, ['run', 'test', `-- --package-name '*'`], {
expect.assertions(5);
const { stdout } = await Shell.exec(`npm`, ['run', 'test', `-- --package-name '*'`], {
cwd: path.resolve('./packages/_/monorepo'),
stdio: 'pipe'
});

expect(response).toBe(0);
// console.log(stdout);
expect(stdout).toMatch('4 passed');

// coverage
expect(stdout).toMatch(' index/src |');
expect(stdout).toMatch(' shared/src |');
expect(stdout).toMatch(' components |');
expect(stdout).toMatch(' components/button |');
}, 60000);

it('monorepo, subpackages, *', async () => {
expect.assertions(1);
const response = await Shell.spawn(`npm`, ['run', 'test', `-- --package-name '@demo/helper-components'`], {
expect.assertions(3);
const { stdout } = await Shell.exec(`npm`, ['run', 'test', `-- --package-name '@demo/helper-components'`], {
cwd: path.resolve('./packages/_/monorepo'),
stdio: 'pipe'
});

expect(response).toBe(0);
// console.log(stdout);
expect(stdout).toMatch('2 passed');

// coverage
expect(stdout).toMatch(' components |');
expect(stdout).toMatch(' components/button |');
}, 60000);

it('monorepo, subpackages, button', async () => {
expect.assertions(1);
const response = await Shell.spawn(`npm`, ['run', 'test', `-- --package-name '@demo/helper-components' --subpackage 'button'`], {
expect.assertions(2);
const { stdout } = await Shell.exec(`npm`, ['run', 'test', `-- --package-name '@demo/helper-components' --subpackage 'button'`], {
cwd: path.resolve('./packages/_/monorepo'),
stdio: 'pipe'
});

expect(response).toBe(0);
// console.log(stdout);
expect(stdout).toMatch(`1 passed`);
expect(stdout).toMatch(' index.ts |');
}, 60000);

it('singlerepo', async () => {
Expand Down
59 changes: 43 additions & 16 deletions packages/tester/shared.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,50 @@ const cwd = process.cwd();

// options
const options = JSON.parse(decodeURIComponent(process.env.TEST_OPTIONS || '{}'));
const { workspace, packageFolderName, subpackageFolderName, subpackages } = options;
const { workspace, packageFolderName, subpackageFolderName, subpackagesMap } = options;

const testDirPrefix = workspace
? `${workspace}/${packageFolderName || '*'}${subpackages.length ? subpackageFolderName ? `/${subpackageFolderName}` : '/**' : ''}/__tests__`
: `__tests__`;
let tests: string[] = [];
let collects: string[] = [];

const collectDirPrefix = workspace
// eslint-disable-next-line max-len
? `${workspace}/${packageFolderName || '*'}/${subpackages.length ? subpackageFolderName ? `${subpackageFolderName}` : '' : 'src'}`
: ``;
const TEST_PATTEN = `**.(spec|test).[jt]s?(x)`;
const COLLECT_PATTEN = `**/*.ts`;

if (workspace) {
let prefixDir = `${workspace}/${packageFolderName || '*'}`;
tests.push(`${prefixDir}/__tests__/${TEST_PATTEN}`);
collects.push(`${prefixDir}/src/${COLLECT_PATTEN}`);

if (packageFolderName === '*') {
Object.keys(subpackagesMap).forEach((packageFolderName$: string) => {
let subpackages = subpackagesMap[packageFolderName$];
if (subpackages.length) {
let prefixDir$ = `${workspace}/${packageFolderName$}`;
let subpackagesPatten = `{${subpackages.join(',')},}`;

tests.push(`${prefixDir$}/${subpackagesPatten}/__tests__/${TEST_PATTEN}`);
collects.push(`${prefixDir$}/${subpackagesPatten}/${COLLECT_PATTEN}`);
collects.push(`${prefixDir$}/index*.ts`);
}
});
} else if (subpackagesMap[packageFolderName].length) {
if (subpackageFolderName) {
tests = [];
collects = [];

tests.push(`${prefixDir}/${subpackageFolderName}/__tests__/${TEST_PATTEN}`);
collects.push(`${prefixDir}/${subpackageFolderName}/${COLLECT_PATTEN}`);
} else {
let subpackages = subpackagesMap[packageFolderName];
let subpackagesPatten = `{${subpackages.join(',')},}`;
tests.push(`${prefixDir}/${subpackagesPatten}/__tests__/${TEST_PATTEN}`);
collects.push(`${prefixDir}/${subpackagesPatten}/${COLLECT_PATTEN}`);
collects.push(`${prefixDir}/index*.ts`);
}
}
} else {
tests.push(`__tests__/${TEST_PATTEN}`);
collects.push(`src/${COLLECT_PATTEN}`);
}
// alias
const replacement = (name: string) => path.resolve(cwd, `./packages/${name}/src`);
const { name } = createRequire(cwd)(path.resolve(cwd, workspace ? `${workspace}/index` : '', 'package.json'));
Expand All @@ -41,9 +73,7 @@ export default defineConfig({
: {},
test: {
globals: true,
include: [
`${testDirPrefix}/**.(spec|test).[jt]s?(x)`
],
include: tests,
coverage: {
enabled: true,
provider: 'istanbul',
Expand All @@ -52,12 +82,9 @@ export default defineConfig({
statements: 95,
functions: 95,
lines: 95,
include: [
`${collectDirPrefix}/**/*.ts`
],
include: collects,
exclude: [
`__tests__/**/*`,
`examples/**/*`,
`**/examples/**`,
...configDefaults.coverage.exclude!
]
}
Expand Down
2 changes: 1 addition & 1 deletion packages/tester/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const run = (options: Options) => Utils.autoCatch(async () => {
options.workspace = workspace;

options.subpackageFolderName = options.subpackageFolderName || options.subpackage;
options.subpackages = subpackagesMap[options.packageFolderName];
options.subpackagesMap = subpackagesMap;

const packageOptions = packageOptionsMap[options.packageFolderName];
const packageDir = packageDirsMap[options.packageFolderName];
Expand Down

0 comments on commit 064fe74

Please sign in to comment.