Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LPD-50000 - Add script to run all js unit tests at once #4762

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions modules/_node-scripts/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,16 @@ const COMMANDS = {
},
'test': {
description: `
Runs unit tests in a single or multiple projects.

When multiple projects are tested and --sync argument is given, project tests are run
serially.
Runs unit tests in a single project.
`,
parameters: '[--sync]',
script: './test/index.mjs',
},
'test:all': {
description: `
Runs all unit tests across all projects.
`,
script: './test/all.mjs',
},
};

const command = process.argv[2];
Expand Down
2 changes: 1 addition & 1 deletion modules/_node-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"node-scripts": "./bin.js"
},
"com.liferay": {
"sha256": "18c2360336c0f189cd862741b8b7be682192caec193c05c53a2c226f299e9045"
"sha256": "8dab917273d07cab3c10b770979e36fd3de958e9d13e962400e6c7355bead4c4"
},
"dependencies": {
"@babel/preset-env": "7.24.7",
Expand Down
56 changes: 56 additions & 0 deletions modules/_node-scripts/test/all.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/

import {$} from 'execa';
import fs from 'fs/promises';
import path from 'path';

import getYarnWorkspaceProjects from '../util/getYarnWorkspaceProjects.mjs';
import runConcurrentTasks from '../util/runConcurrentTasks.mjs';

export default async function all() {
const projects = await getYarnWorkspaceProjects();

const testableProjects = [];

/**
* Filter out projects that do not have `node-scripts test`
*/
for (const projectPath of projects) {
const packageJson = path.join(projectPath, 'package.json');
const pkgJsonContents = await fs.readFile(packageJson, 'utf8');

if (
pkgJsonContents.includes('node-scripts test') &&
!pkgJsonContents.includes('node-scripts test:all')
) {
testableProjects.push(projectPath);
}
}

console.log(`ℹ️ Testing ${testableProjects.length} projects.`);

const tasks = testableProjects.map((project) => async () => {
console.log(`🧪 Testing ${project}`);

const {failed} = await $({
cwd: project,
env: {
...process.env,
NODE_ENV: 'test',
},
reject: false,
stdio: 'pipe',
})`yarn run test`;

console.log(
`${!failed ? '✅ PASSED' : '❌ FAILED'} ${path.basename(project)}`
);

return all;
});

await runConcurrentTasks(tasks);
}
137 changes: 6 additions & 131 deletions modules/_node-scripts/test/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,139 +3,14 @@
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/

import fs from 'fs/promises';
import path from 'path';

import getNamedArguments from '../util/getNamedArguments.mjs';
import getYarnWorkspaceProjects from '../util/getYarnWorkspaceProjects.mjs';
import runConcurrentTasks, {
MAX_CONCURRENT_TASKS,
} from '../util/runConcurrentTasks.mjs';
import runJest from './jest/runJest.mjs';

export default async function () {
const {sync} = getNamedArguments({
sync: '--sync',
await runJest({
cliFlags: process.argv.slice(3),
cwd: process.cwd(),
execaConfig: {
stdio: 'inherit',
},
});

const originalNodeEnv = process.env.NODE_ENV;

process.env.NODE_ENV = 'test';

const args = process.argv.slice(3);

/**
* When using 'yarn run ...' it sets the cwd to the nearest package.json
*/
let cwd = process.env.INIT_CWD;

if (!cwd) {
cwd = process.cwd();
}

const projects = await getYarnWorkspaceProjects();

/**
* Map containing the path to the project and the environment variables
* to be used when running the tests.
*/
const testableProjectsMap = new Map();

/**
* Filter out projects that do not have `node-scripts test`
*/
for (const projectPath of projects) {

// Check if deeply nested passed a project root or check if shallowly
// nested before several project roots

if (
cwd.includes(projectPath) ||
projectPath.includes(process.env.INIT_CWD)
) {
const packageJson = path.join(projectPath, 'package.json');
const pkgJsonContents = await fs.readFile(packageJson, 'utf8');

if (pkgJsonContents.includes('node-scripts test')) {
const pkgJson = JSON.parse(pkgJsonContents);

testableProjectsMap.set(
projectPath,
getEnvVars(pkgJson.scripts.test)
);
}
}
}

const totalTestableProjects = testableProjectsMap.size;

if (totalTestableProjects === 1) {
const [[projectPath, envObj]] = testableProjectsMap.entries();

await runJest({
cliFlags: args,
cwd: projectPath,
execaConfig: {
env: envObj,
stdio: 'inherit',
},
});
}
else {
console.log(
`ℹ️ Testing ${totalTestableProjects} projects in ${sync ? 'series' : 'parallel'}.`
);

const asyncItems = [];

for (const [projectPath, envObj] of testableProjectsMap) {
asyncItems.push(async (stdio = 'pipe') => {
console.log(`🧪 Testing ${path.basename(projectPath)}`);

const {all, failed} = await runJest({
cliFlags: args,
cwd: projectPath,
execaConfig: {
all: true,
env: envObj,
reject: false,
stdio,
},
});

console.log(
`${!failed ? '✅ PASSED' : '❌ FAILED'} ${path.basename(projectPath)}`
);

return all;
});
}

if (sync) {
for (const task of asyncItems) {
await task('inherit');
}
}
else {
console.log(`> Running in groups of ${MAX_CONCURRENT_TASKS}.`);

const results = await runConcurrentTasks(asyncItems);

console.log(results.join('\n'));
}
}

process.env.NODE_ENV = originalNodeEnv;
}

function getEnvVars(value) {
return value
.split(' ')
.filter((part) => part.includes('='))
.reduce((acc, part) => {
const [key, value] = part.split('=');
acc[key] = value;

return acc;
}, {});
}
14 changes: 0 additions & 14 deletions modules/_node-scripts/test/sync.mjs

This file was deleted.

2 changes: 1 addition & 1 deletion modules/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"format": "node-scripts format",
"formatCurrentBranch": "node-scripts format --current-branch",
"formatLocalChanges": "node-scripts format --local-changes",
"test:all": "node-scripts test"
"testAll": "node-scripts test:all"
},
"workspaces": {
"packages": [
Expand Down
4 changes: 4 additions & 0 deletions portal-impl/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,10 @@ svn://svn.liferay.com/repos/public/alloy/trunk/sandbox/taglibs.
</for>
</target>

<target name="js-unit-all">
<gradle-execute dir="${project.dir}/modules" task="packageRunTestAll" />
</target>

<target name="prepare-upgrade-table">
<for param="zip.file">
<path>
Expand Down