Skip to content

Commit 06587b4

Browse files
committed
chore: migrate core/common tests to jest
1 parent b8123ca commit 06587b4

13 files changed

Lines changed: 1305 additions & 1574 deletions

File tree

core/common/.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
**/*.log
22
**/node_modules
3-
.coverage
4-
.nyc_output
3+
coverage/
54
docs/
65
out/
76
system-test/secrets.js

core/common/.mocharc.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

core/common/.nycrc

Lines changed: 0 additions & 24 deletions
This file was deleted.

core/common/jest.config.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module.exports = {
2+
preset: 'ts-jest',
3+
testEnvironment: 'node',
4+
testMatch: ['<rootDir>/test/*.ts'],
5+
testPathIgnorePatterns: ['/node_modules/', '/build/'],
6+
collectCoverage: true,
7+
coverageDirectory: 'coverage',
8+
coverageReporters: ['text', 'lcov'],
9+
transform: {
10+
'^.+\\.tsx?$': [
11+
'ts-jest',
12+
{
13+
tsconfig: 'tsconfig.json',
14+
},
15+
],
16+
},
17+
};

core/common/jest.config.system.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
preset: 'ts-jest',
3+
testEnvironment: 'node',
4+
testMatch: ['<rootDir>/system-test/*.ts'],
5+
testPathIgnorePatterns: ['/node_modules/', '/build/'],
6+
transform: {
7+
'^.+\\.tsx?$': [
8+
'ts-jest',
9+
{
10+
tsconfig: 'tsconfig.json',
11+
},
12+
],
13+
},
14+
};

core/common/package.json

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,16 @@
2020
],
2121
"scripts": {
2222
"docs": "jsdoc -c .jsdoc.js",
23-
"test": "c8 mocha build/test",
23+
"test": "node --experimental-vm-modules node_modules/.bin/jest",
2424
"prepare": "npm run compile",
2525
"pretest": "npm run compile",
2626
"compile": "tsc -p .",
2727
"fix": "gts fix",
2828
"lint": "gts check",
2929
"presystem-test": "npm run compile",
30-
"system-test": "mocha build/system-test",
31-
"samples-test": "cd samples/ && npm link ../ && npm test && cd ../",
30+
"system-test": "node --experimental-vm-modules node_modules/.bin/jest --config jest.config.system.js --runInBand",
3231
"docs-test": "linkinator docs",
3332
"predocs-test": "npm run docs",
34-
"prelint": "cd samples; npm link ../; npm install",
3533
"clean": "gts clean",
3634
"precompile": "gts clean"
3735
},
@@ -49,28 +47,23 @@
4947
"devDependencies": {
5048
"@types/ent": "^2.2.8",
5149
"@types/extend": "^3.0.4",
52-
"@types/mocha": "^10.0.10",
50+
"@types/jest": "^29.5.12",
5351
"@types/mv": "^2.1.4",
5452
"@types/ncp": "^2.0.8",
5553
"@types/node": "^22.13.5",
56-
"@types/proxyquire": "^1.3.31",
5754
"@types/request": "^2.48.12",
58-
"@types/sinon": "^17.0.4",
5955
"@types/tmp": "^0.2.6",
60-
"c8": "^10.1.3",
61-
"codecov": "^3.8.3",
6256
"gts": "^6.0.2",
57+
"jest": "^29.7.0",
6358
"jsdoc": "^4.0.4",
6459
"jsdoc-fresh": "^3.0.0",
6560
"jsdoc-region-tag": "^3.0.0",
6661
"linkinator": "^6.1.2",
67-
"mocha": "^11.1.0",
6862
"mv": "^2.1.1",
6963
"ncp": "^2.0.0",
7064
"nock": "^14.0.1",
71-
"proxyquire": "^2.1.3",
72-
"sinon": "^19.0.2",
7365
"tmp": "^0.2.3",
66+
"ts-jest": "^29.2.5",
7467
"typescript": "^5.8.2"
7568
},
7669
"homepage": "https://github.com/googleapis/google-cloud-node/tree/main/core/common"

core/common/system-test/common.ts

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,53 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import {before, describe, it} from 'mocha';
16-
import * as assert from 'assert';
1715
import * as http from 'http';
18-
1916
import * as common from '../src';
17+
import {util} from '../src/util';
2018

2119
describe('Common', () => {
2220
const MOCK_HOST_PORT = 8118;
2321
const MOCK_HOST = `http://localhost:${MOCK_HOST_PORT}`;
2422

2523
describe('Service', () => {
2624
let service: common.Service;
25+
let mockServer: http.Server | undefined;
26+
27+
beforeAll(() => {
28+
jest.spyOn(util, 'makeAuthenticatedRequestFactory').mockImplementation((config: any) => {
29+
const makeAuthenticatedRequest: any = (reqOpts: any, callback: any) => {
30+
if (typeof callback === 'function') {
31+
return util.makeRequest(reqOpts, config, callback);
32+
}
33+
return util.makeRequest(reqOpts, config, () => {});
34+
};
35+
makeAuthenticatedRequest.authClient = {
36+
getProjectId: async () => 'project-id'
37+
};
38+
return makeAuthenticatedRequest;
39+
});
2740

28-
before(() => {
2941
service = new common.Service({
3042
baseUrl: MOCK_HOST,
3143
apiEndpoint: MOCK_HOST,
3244
scopes: [],
3345
packageJson: {name: 'tests', version: '1.0.0'},
46+
projectIdRequired: false,
3447
});
3548
});
3649

50+
afterEach(done => {
51+
if (mockServer) {
52+
mockServer.close(() => done());
53+
mockServer = undefined;
54+
} else {
55+
done();
56+
}
57+
});
58+
3759
it('should send a request and receive a response', done => {
3860
const mockResponse = 'response';
39-
const mockServer = new http.Server((req, res) => {
61+
mockServer = new http.Server((req, res) => {
4062
res.end(mockResponse);
4163
});
4264

@@ -47,19 +69,21 @@ describe('Common', () => {
4769
uri: '/mock-endpoint',
4870
},
4971
(err, resp) => {
50-
assert.ifError(err);
51-
assert.strictEqual(resp, mockResponse);
52-
mockServer.close(done);
72+
try {
73+
expect(err).toBeNull();
74+
expect(resp).toBe(mockResponse);
75+
done();
76+
} catch (e) {
77+
done(e);
78+
}
5379
},
5480
);
5581
});
5682

57-
it('should retry a request', function (done) {
58-
this.timeout(60 * 1000);
59-
83+
it('should retry a request', done => {
6084
let numRequestAttempts = 0;
6185

62-
const mockServer = new http.Server((req, res) => {
86+
mockServer = new http.Server((req, res) => {
6387
numRequestAttempts++;
6488
res.statusCode = 408;
6589
res.end();
@@ -70,18 +94,21 @@ describe('Common', () => {
7094
service.request(
7195
{
7296
uri: '/mock-endpoint-retry',
97+
maxRetries: 4,
7398
},
7499
err => {
75-
assert.strictEqual((err! as common.ApiError).code, 408);
76-
assert.strictEqual(numRequestAttempts, 4);
77-
mockServer.close(done);
100+
try {
101+
expect((err! as common.ApiError).code).toBe(408);
102+
expect(numRequestAttempts).toBe(4);
103+
done();
104+
} catch (e) {
105+
done(e);
106+
}
78107
},
79108
);
80-
});
81-
82-
it('should retry non-responsive hosts', function (done) {
83-
this.timeout(60 * 1000);
109+
}, 60000);
84110

111+
it('should retry non-responsive hosts', done => {
85112
function getMinimumRetryDelay(retryNumber: number) {
86113
return Math.pow(2, retryNumber) * 1000;
87114
}
@@ -100,12 +127,16 @@ describe('Common', () => {
100127
uri: '/mock-endpoint-no-response',
101128
},
102129
err => {
103-
assert(err?.message.includes('ECONNREFUSED'));
104-
const timeResponse = Date.now();
105-
assert(timeResponse - timeRequest > minExpectedResponseTime);
106-
done();
130+
try {
131+
expect(err?.message).toMatch(/ECONNREFUSED|ENOTFOUND|failed, reason:/);
132+
const timeResponse = Date.now();
133+
expect(timeResponse - timeRequest).toBeGreaterThan(minExpectedResponseTime);
134+
done();
135+
} catch (e) {
136+
done(e);
137+
}
107138
},
108139
);
109-
});
140+
}, 60000);
110141
});
111142
});

core/common/system-test/install.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@ import {ncp} from 'ncp';
1818
import * as os from 'os';
1919
import * as tmp from 'tmp';
2020
import {promisify} from 'util';
21-
import {describe, it, after} from 'mocha';
2221

23-
const mvp = promisify(mv) as {} as (...args: string[]) => Promise<void>;
22+
const mvp = promisify(mv) as any as (...args: string[]) => Promise<void>;
2423
const ncpp = promisify(ncp);
2524
const keep = !!process.env.KEEP_TEMPDIRS;
2625
const stagingDir = tmp.dirSync({keep, unsafeCleanup: true});
2726
const stagingPath = stagingDir.name;
2827
// eslint-disable-next-line @typescript-eslint/no-var-requires
29-
const pkg = require('../../package.json');
28+
const pkg = require('../package.json');
3029
const pkgName = 'google-cloud-common';
3130
const npm = os.platform() === 'win32' ? 'npm.cmd' : 'npm';
3231

@@ -62,12 +61,12 @@ describe('install tests', () => {
6261
await mvp(tarball, `${stagingPath}/${pkgName}.tgz`);
6362
await ncpp('system-test/fixtures/kitchen', `${stagingPath}/`);
6463
await spawnp(npm, ['install'], {cwd: `${stagingPath}/`});
65-
}).timeout(120000);
64+
}, 120000);
6665

6766
/**
6867
* CLEAN UP - remove the staging directory when done.
6968
*/
70-
after('cleanup staging', async () => {
69+
afterAll(async () => {
7170
if (!keep) {
7271
stagingDir.removeCallback();
7372
}

core/common/test/index.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,13 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import * as assert from 'assert';
16-
import {describe, it} from 'mocha';
1715
import {Operation, Service, ServiceObject, util} from '../src';
1816

1917
describe('common', () => {
2018
it('should correctly export the common modules', () => {
21-
assert(Operation);
22-
assert(Service);
23-
assert(ServiceObject);
24-
assert(util);
19+
expect(Operation).toBeDefined();
20+
expect(Service).toBeDefined();
21+
expect(ServiceObject).toBeDefined();
22+
expect(util).toBeDefined();
2523
});
2624
});

0 commit comments

Comments
 (0)