Skip to content

Commit 1c808a4

Browse files
committed
properly normalize SDK and Node version numbers in tests
1 parent 3c7bae8 commit 1c808a4

File tree

4 files changed

+33
-72
lines changed

4 files changed

+33
-72
lines changed

src/common/utils/runtime-info.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { detectRuntime } from './env';
22

33
// Extend globalThis to include runtime-specific globals
44
declare global {
5-
// eslint-disable-next-line no-var
65
var Deno:
76
| {
87
version: {
@@ -11,7 +10,6 @@ declare global {
1110
}
1211
| undefined;
1312

14-
// eslint-disable-next-line no-var
1513
var Bun:
1614
| {
1715
version: string;

src/sso/__snapshots__/sso.spec.ts.snap

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,6 @@ exports[`SSO SSO getAuthorizationUrl with state generates an authorize url with
1919
exports[`SSO SSO getProfileAndToken with all information provided sends a request to the WorkOS api for a profile 1`] = `"client_id=proj_123&client_secret=sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU&grant_type=authorization_code&code=authorization_code"`;
2020

2121
exports[`SSO SSO getProfileAndToken with all information provided sends a request to the WorkOS api for a profile 2`] = `
22-
{
23-
"Accept": "application/json, text/plain, */*",
24-
"Authorization": "Bearer sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU",
25-
"Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
26-
"User-Agent": "workos-node/VERSION/fetch (node/v18.0.0)",
27-
}
28-
`;
29-
30-
exports[`SSO SSO getProfileAndToken with all information provided sends a request to the WorkOS api for a profile 3`] = `
3122
{
3223
"connectionId": "conn_123",
3324
"connectionType": "OktaSAML",
@@ -63,15 +54,6 @@ exports[`SSO SSO getProfileAndToken with all information provided sends a reques
6354
exports[`SSO SSO getProfileAndToken without a groups attribute sends a request to the WorkOS api for a profile 1`] = `"client_id=proj_123&client_secret=sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU&grant_type=authorization_code&code=authorization_code"`;
6455

6556
exports[`SSO SSO getProfileAndToken without a groups attribute sends a request to the WorkOS api for a profile 2`] = `
66-
{
67-
"Accept": "application/json, text/plain, */*",
68-
"Authorization": "Bearer sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU",
69-
"Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
70-
"User-Agent": "workos-node/VERSION/fetch (node/v18.0.0)",
71-
}
72-
`;
73-
74-
exports[`SSO SSO getProfileAndToken without a groups attribute sends a request to the WorkOS api for a profile 3`] = `
7557
{
7658
"connectionId": "conn_123",
7759
"connectionType": "OktaSAML",

src/sso/sso.spec.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -302,14 +302,19 @@ describe('SSO', () => {
302302
expect(fetch.mock.calls.length).toEqual(1);
303303

304304
expect(fetchBody()).toMatchSnapshot();
305+
305306
const headers = fetchHeaders() as Record<string, string>;
306-
const normalizedHeaders = {
307-
...headers,
308-
'User-Agent': headers['User-Agent']
309-
.replace(/workos-node\/[^\s/]+/, 'workos-node/VERSION')
310-
.replace(/\(node\/v[\d.]+\)/, '(node/v18.0.0)'),
311-
};
312-
expect(normalizedHeaders).toMatchSnapshot();
307+
expect(headers['Accept']).toBe('application/json, text/plain, */*');
308+
expect(headers['Authorization']).toBe(
309+
'Bearer sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU',
310+
);
311+
expect(headers['Content-Type']).toBe(
312+
'application/x-www-form-urlencoded;charset=utf-8',
313+
);
314+
expect(headers['User-Agent']).toMatch(
315+
/^workos-node\/\d+\.\d+\.\d+(-[a-zA-Z0-9.]+)?\/fetch \(node\/v\d+\.\d+\.\d+\)$/,
316+
);
317+
313318
expect(accessToken).toBe('01DMEK0J53CVMC32CK5SE0KZ8Q');
314319
expect(profile).toMatchSnapshot();
315320
});
@@ -349,14 +354,19 @@ describe('SSO', () => {
349354
expect(fetch.mock.calls.length).toEqual(1);
350355

351356
expect(fetchBody()).toMatchSnapshot();
357+
352358
const headers = fetchHeaders() as Record<string, string>;
353-
const normalizedHeaders = {
354-
...headers,
355-
'User-Agent': headers['User-Agent']
356-
.replace(/workos-node\/[^\s/]+/, 'workos-node/VERSION')
357-
.replace(/\(node\/v[\d.]+\)/, '(node/v18.0.0)'),
358-
};
359-
expect(normalizedHeaders).toMatchSnapshot();
359+
expect(headers['Accept']).toBe('application/json, text/plain, */*');
360+
expect(headers['Authorization']).toBe(
361+
'Bearer sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU',
362+
);
363+
expect(headers['Content-Type']).toBe(
364+
'application/x-www-form-urlencoded;charset=utf-8',
365+
);
366+
expect(headers['User-Agent']).toMatch(
367+
/^workos-node\/\d+\.\d+\.\d+(-[a-zA-Z0-9.]+)?\/fetch \(node\/v\d+\.\d+\.\d+\)$/,
368+
);
369+
360370
expect(accessToken).toBe('01DMEK0J53CVMC32CK5SE0KZ8Q');
361371
expect(profile).toMatchSnapshot();
362372
});

src/workos.spec.ts

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import fetch from 'jest-fetch-mock';
22
import { fetchOnce, fetchHeaders, fetchBody } from './common/utils/test-utils';
3-
import fs from 'fs/promises';
43
import {
54
GenericServerException,
65
NoApiKeyProvidedException,
@@ -107,10 +106,6 @@ describe('WorkOS', () => {
107106
it('applies the configuration to the fetch client user-agent', async () => {
108107
fetchOnce('{}');
109108

110-
const packageJson = JSON.parse(
111-
await fs.readFile('package.json', 'utf8'),
112-
);
113-
114109
const workos = new WorkOS('sk_test', {
115110
appInfo: {
116111
name: 'fooApp',
@@ -120,45 +115,25 @@ describe('WorkOS', () => {
120115

121116
await workos.post('/somewhere', {});
122117

123-
expect(fetchHeaders()).toMatchObject({
124-
'User-Agent': `workos-node/${packageJson.version}/fetch (node/v18.20.7) fooApp: 1.0.0`,
125-
});
126-
});
127-
});
128-
129-
describe('when no `appInfo` option is provided', () => {
130-
it('adds the HTTP client name to the user-agent', async () => {
131-
fetchOnce('{}');
132-
133-
const packageJson = JSON.parse(
134-
await fs.readFile('package.json', 'utf8'),
118+
const headers = fetchHeaders() as Record<string, string>;
119+
expect(headers['User-Agent']).toMatch(
120+
/^workos-node\/\d+\.\d+\.\d+(-[a-zA-Z0-9.]+)?\/fetch \(node\/v\d+\.\d+\.\d+\) fooApp: 1\.0\.0$/,
135121
);
136-
137-
const workos = new WorkOS('sk_test');
138-
139-
await workos.post('/somewhere', {});
140-
141-
expect(fetchHeaders()).toMatchObject({
142-
'User-Agent': `workos-node/${packageJson.version}/fetch (node/v18.20.7)`,
143-
});
144122
});
145123
});
146124

147125
describe('when no `appInfo` option is provided', () => {
148126
it('adds the HTTP client name to the user-agent', async () => {
149127
fetchOnce('{}');
150128

151-
const packageJson = JSON.parse(
152-
await fs.readFile('package.json', 'utf8'),
153-
);
154-
155129
const workos = new WorkOS('sk_test');
156130

157131
await workos.post('/somewhere', {});
158132

159-
expect(fetchHeaders()).toMatchObject({
160-
'User-Agent': `workos-node/${packageJson.version}/fetch (node/v18.20.7)`,
161-
});
133+
const headers = fetchHeaders() as Record<string, string>;
134+
expect(headers['User-Agent']).toMatch(
135+
/^workos-node\/\d+\.\d+\.\d+(-[a-zA-Z0-9.]+)?\/fetch \(node\/v\d+\.\d+\.\d+\)$/,
136+
);
162137
});
163138
});
164139

@@ -174,14 +149,10 @@ describe('WorkOS', () => {
174149
});
175150

176151
describe('version', () => {
177-
it('matches the version in `package.json`', async () => {
152+
it('is a valid semver string', async () => {
178153
const workos = new WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU');
179154

180-
// Read `package.json` using file I/O instead of `require` so we don't run
181-
// into issues with the `require` cache.
182-
const packageJson = JSON.parse(await fs.readFile('package.json', 'utf8'));
183-
184-
expect(workos.version).toBe(packageJson.version);
155+
expect(workos.version).toMatch(/^\d+\.\d+\.\d+(-[a-zA-Z0-9.]+)?$/);
185156
});
186157
});
187158

0 commit comments

Comments
 (0)