Skip to content

Commit f60f3e0

Browse files
test: improved local test running (#1441)
- Added node script to add terminal aliases (helps to easily run test groups) - Integrated mocha watch - Removed redundant sh helper scripts - See mochajs/mocha#5149 --------- Co-authored-by: Arya <[email protected]>
1 parent d4999de commit f60f3e0

33 files changed

+145
-170
lines changed

Diff for: CONTRIBUTING.md

+22-4
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,33 @@ Install the [`aws-cli`](https://docs.aws.amazon.com/cli/latest/userguide/getting
2626

2727
## Executing Tests Locally
2828

29-
Some of the tests require infrastructure components (databases etc.) to run locally. The easiest way to run all required components locally is to use Docker and on top of this [Docker Compose](https://docs.docker.com/compose/). Start the script `node bin/start-test-containers.js` to run all containers (not recommended).
30-
31-
Instead:
29+
Some of the tests require infrastructure components (databases etc.) to run locally. The easiest way to run required components locally is to use Docker and on top of this [Docker Compose](https://docs.docker.com/compose/).
3230

3331
```sh
3432
node bin/start-test-containers.js --mongo --redis
3533
```
3634

37-
It's not recommended to run all tests using `bin/run-tests.sh` - it takes too long. Take a look at the root package.json and run a specific test group or run e.g. `bin/run-collector-tests.sh` with the mocha `.only` attribute.
35+
### Using terminal aliases
36+
37+
Add aliases to your terminal:
38+
39+
```sh
40+
node bin/add-test-aliases.js bash|zsh
41+
```
42+
43+
Add a mocha `.only` on the test you would like to execute and then run the target scope:
44+
45+
```sh
46+
runcollector # Run the 'collector' scope with watch mode
47+
runcollector-nw # Run the 'collector' scope without watch mode
48+
```
49+
50+
### Manually
51+
52+
```sh
53+
bin/run-tests.sh --scope=@instana/collector
54+
bin/run-tests.sh --scope=@instana/collector --watch
55+
```
3856

3957
If you want to see the Node.js collector's debug output while running the tests, make sure the environment variable `WITH_STDOUT` is set to a non-empty string.
4058

Diff for: bin/add-test-aliases.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* (c) Copyright IBM Corp. 2024
3+
*/
4+
5+
'use strict';
6+
7+
const { execSync } = require('child_process');
8+
const fs = require('fs');
9+
const os = require('os');
10+
11+
const addAliasIfNotExists = (aliasCommand, configFile) => {
12+
let fileContent = '';
13+
try {
14+
fileContent = fs.readFileSync(configFile, 'utf8');
15+
} catch (err) {
16+
console.error(`Could not read ${configFile}: ${err.message}`);
17+
return;
18+
}
19+
20+
if (!fileContent.includes(aliasCommand)) {
21+
fs.appendFileSync(configFile, `\n${aliasCommand}\n`);
22+
console.log(`Added alias: ${aliasCommand}`);
23+
} else {
24+
console.log(`Alias already exists: ${aliasCommand}`);
25+
}
26+
};
27+
28+
const output = execSync('lerna list --json', { encoding: 'utf-8' });
29+
const packages = JSON.parse(output);
30+
const scopeNames = packages.map(pkg => pkg.name);
31+
const shellArg = process.argv[2];
32+
33+
if (!shellArg) {
34+
console.error('Error: Please specify either "bash" or "zsh".');
35+
process.exit(1);
36+
}
37+
38+
let configFile;
39+
if (shellArg === 'bash') {
40+
configFile = `${os.homedir()}/.bashrc`;
41+
} else if (shellArg === 'zsh') {
42+
configFile = `${os.homedir()}/.zshrc`;
43+
} else {
44+
console.error('Error: Invalid argument. Please specify "bash" or "zsh".');
45+
process.exit(1);
46+
}
47+
48+
scopeNames.forEach(scope => {
49+
const cleanedScope = scope.replace('@instana/', '');
50+
51+
const watchAlias = `alias run${cleanedScope}='bin/run-tests.sh --scope=${scope} --watch'`;
52+
const nwAlias = `alias run${cleanedScope}-nw='bin/run-tests.sh --scope=${scope}'`;
53+
54+
addAliasIfNotExists(watchAlias, configFile);
55+
addAliasIfNotExists(nwAlias, configFile);
56+
});
57+
58+
console.log('Aliases added. Please run the following command to apply the changes:');
59+
console.log(` source ${configFile}`);
60+
console.log('Alternatively, restart your terminal.');
61+
62+
console.log('Done');

Diff for: bin/run-autoprofile-tests.sh

-11
This file was deleted.

Diff for: bin/run-aws-lambda-auto-wrap.sh

-10
This file was deleted.

Diff for: bin/run-azure-container-services-tests.sh

-9
This file was deleted.

Diff for: bin/run-collector-tests.sh

-11
This file was deleted.

Diff for: bin/run-core-tests.sh

-11
This file was deleted.

Diff for: bin/run-fargate-tests.sh

-11
This file was deleted.

Diff for: bin/run-google-cloud-run-tests.sh

-11
This file was deleted.

Diff for: bin/run-lambda-tests.sh

-11
This file was deleted.

Diff for: bin/run-metrics-util-tests.sh

-11
This file was deleted.

Diff for: bin/run-otel-exporter-tests.sh

-11
This file was deleted.

Diff for: bin/run-otel-sampler-tests.sh

-9
This file was deleted.

Diff for: bin/run-serverless-collector-tests.sh

-10
This file was deleted.

Diff for: bin/run-serverless-tests.sh

-11
This file was deleted.

Diff for: bin/run-shared-metrics-tests.sh

-11
This file was deleted.

Diff for: bin/run-tests.sh

+27-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,35 @@
22

33
#######################################
44
# (c) Copyright IBM Corp. 2021
5-
# (c) Copyright Instana Inc. and contributors 2018
65
#######################################
76

87
set -eo pipefail
98

10-
npx lerna run test:debug --stream
9+
WATCH=false
10+
SCOPE=""
11+
12+
for arg in "$@"; do
13+
case $arg in
14+
--watch)
15+
WATCH=true
16+
shift
17+
;;
18+
--scope=*)
19+
SCOPE="${arg#*=}"
20+
shift
21+
;;
22+
*)
23+
shift
24+
;;
25+
esac
26+
done
27+
28+
if [ "$WATCH" = true ]; then
29+
export npm_config_watch="--watch"
30+
else
31+
export npm_config_watch=""
32+
fi
33+
34+
echo "Running tests with $SCOPE and $npm_config_watch:"
35+
npx lerna exec --scope="$SCOPE" "npm run test:debug"
1136

Diff for: packages/autoprofile/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"audit": "npm audit --omit=dev",
2828
"node_modules:exists": "mkdir -p node_modules",
2929
"install": "node-gyp-build",
30-
"test": "mocha --config=test/.mocharc.js --require test/hooks.js --sort $(find test -iname '*test.js' -not -path '*node_modules*')",
30+
"test": "mocha $npm_config_watch --config=test/.mocharc.js --require test/hooks.js --sort $(find test -iname '*test.js' -not -path '*node_modules*')",
3131
"test:debug": "WITH_STDOUT=true npm run test",
3232
"test:ci": "echo \"******* Files to be tested:\n $CI_AUTOPROFILE_TEST_FILES\" && if [ -z \"${CI_AUTOPROFILE_TEST_FILES}\" ]; then echo \"No test files have been assigned to this CircleCI executor.\"; else mocha --config=test/.mocharc.js --reporter mocha-multi-reporters --reporter-options configFile=reporter-config.json --require test/hooks.js --sort ${CI_AUTOPROFILE_TEST_FILES}; fi",
3333
"lint": "eslint lib test",

Diff for: packages/aws-fargate/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"scripts": {
2828
"audit": "npm audit --omit=dev",
2929
"node_modules:exists": "mkdir -p node_modules",
30-
"test": "mocha --config=test/.mocharc.js --reporter mocha-multi-reporters --reporter-options configFile=reporter-config.json --sort $(find test -iname '*test.js')",
30+
"test": "mocha $npm_config_watch --config=test/.mocharc.js --reporter mocha-multi-reporters --reporter-options configFile=reporter-config.json --sort $(find test -iname '*test.js')",
3131
"test:debug": "WITH_STDOUT=true npm run test",
3232
"test:ci": "mocha --config=test/.mocharc.js --reporter mocha-multi-reporters --reporter-options configFile=reporter-config.json 'test/**/*test.js'",
3333
"lint": "eslint src test images",

Diff for: packages/aws-lambda-auto-wrap/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"scripts": {
2828
"audit": "npm audit --omit=dev",
2929
"node_modules:exists": "mkdir -p node_modules",
30-
"test": "mocha --config=test/.mocharc.js --sort $(find test -iname '*test.js')",
30+
"test": "mocha $npm_config_watch --config=test/.mocharc.js --sort $(find test -iname '*test.js')",
3131
"test:debug": "WITH_STDOUT=true npm run test",
3232
"test:ci": "mocha --config=test/.mocharc.js --reporter mocha-multi-reporters --reporter-options configFile=reporter-config.json 'test/**/*test.js'",
3333
"lint": "eslint src",

Diff for: packages/aws-lambda/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"scripts": {
2828
"audit": "npm audit --omit=dev",
2929
"node_modules:exists": "mkdir -p node_modules",
30-
"test": "mocha --config=test/.mocharc.js --sort $(find test -iname '*test.js')",
30+
"test": "mocha $npm_config_watch --config=test/.mocharc.js --sort $(find test -iname '*test.js')",
3131
"test:debug": "WITH_STDOUT=true INSTANA_DEBUG=true npm run test",
3232
"test:ci": "mocha --config=test/.mocharc.js --reporter mocha-multi-reporters --reporter-options configFile=reporter-config.json 'test/**/*test.js'",
3333
"lint": "eslint src test lambdas",

Diff for: packages/azure-container-services/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"scripts": {
2828
"audit": "npm audit --omit=dev",
2929
"node_modules:exists": "mkdir -p node_modules",
30-
"test": "mocha --sort $(find test -iname '*test.js')",
30+
"test": "mocha $npm_config_watch --config=test/.mocharc.js --sort $(find test -iname '*test.js')",
3131
"test:debug": "WITH_STDOUT=true npm run test",
3232
"test:ci": "mocha --config=test/.mocharc.js --reporter mocha-multi-reporters --reporter-options configFile=reporter-config.json 'test/**/*test.js'",
3333
"lint": "eslint src test images",

Diff for: packages/collector/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
},
3737
"scripts": {
3838
"audit": "npm audit --omit=dev",
39-
"test": "USE_OPENTRACING_DEBUG_IMPL=true mocha --config=test/.mocharc.js --require test/hooks.js --sort $(find test -iname '*test.js')",
39+
"test": "USE_OPENTRACING_DEBUG_IMPL=true mocha $npm_config_watch --config=test/.mocharc.js --require test/hooks.js --sort $(find test -iname '*test.js')",
4040
"test:debug": "WITH_STDOUT=true npm run test",
4141
"test:ci:general": "mocha --config=test/.mocharc.js --reporter mocha-multi-reporters --reporter-options configFile=reporter-config.json --require test/hooks.js 'test/**/*test.js' --exclude 'test/tracing/**/*test.js'",
4242
"test:ci:tracing:frameworks": "mocha --config=test/.mocharc.js --reporter mocha-multi-reporters --reporter-options configFile=reporter-config.json --require test/hooks.js 'test/tracing/frameworks/**/*test.js'",

Diff for: packages/collector/test/apps/agentStubControls.js

+3
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ class AgentStubControls {
162162
}
163163

164164
async reset() {
165+
// eslint-disable-next-line no-console
166+
console.log(`[AgentStubControls] reset ${this.agentPort}`);
167+
165168
return retry(async () => {
166169
await fetch(`http://127.0.0.1:${this.agentPort}/`, {
167170
method: 'DELETE',

0 commit comments

Comments
 (0)