Skip to content

Commit fbda123

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

File tree

3 files changed

+18
-71
lines changed

3 files changed

+18
-71
lines changed

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: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -302,14 +302,13 @@ 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('Bearer sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU');
309+
expect(headers['Content-Type']).toBe('application/x-www-form-urlencoded;charset=utf-8');
310+
expect(headers['User-Agent']).toMatch(/^workos-node\/\d+\.\d+\.\d+(-[a-zA-Z0-9.]+)?\/fetch \(node\/v\d+\.\d+\.\d+\)$/);
311+
313312
expect(accessToken).toBe('01DMEK0J53CVMC32CK5SE0KZ8Q');
314313
expect(profile).toMatchSnapshot();
315314
});
@@ -349,14 +348,13 @@ describe('SSO', () => {
349348
expect(fetch.mock.calls.length).toEqual(1);
350349

351350
expect(fetchBody()).toMatchSnapshot();
351+
352352
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();
353+
expect(headers['Accept']).toBe('application/json, text/plain, */*');
354+
expect(headers['Authorization']).toBe('Bearer sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU');
355+
expect(headers['Content-Type']).toBe('application/x-www-form-urlencoded;charset=utf-8');
356+
expect(headers['User-Agent']).toMatch(/^workos-node\/\d+\.\d+\.\d+(-[a-zA-Z0-9.]+)?\/fetch \(node\/v\d+\.\d+\.\d+\)$/);
357+
360358
expect(accessToken).toBe('01DMEK0J53CVMC32CK5SE0KZ8Q');
361359
expect(profile).toMatchSnapshot();
362360
});

src/workos.spec.ts

Lines changed: 6 additions & 39 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,21 @@ 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'),
135-
);
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-
});
118+
const headers = fetchHeaders() as Record<string, string>;
119+
expect(headers['User-Agent']).toMatch(/^workos-node\/\d+\.\d+\.\d+(-[a-zA-Z0-9.]+)?\/fetch \(node\/v\d+\.\d+\.\d+\) fooApp: 1\.0\.0$/);
144120
});
145121
});
146122

147123
describe('when no `appInfo` option is provided', () => {
148124
it('adds the HTTP client name to the user-agent', async () => {
149125
fetchOnce('{}');
150126

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

157129
await workos.post('/somewhere', {});
158130

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

@@ -174,14 +145,10 @@ describe('WorkOS', () => {
174145
});
175146

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

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);
151+
expect(workos.version).toMatch(/^\d+\.\d+\.\d+(-[a-zA-Z0-9.]+)?$/);
185152
});
186153
});
187154

0 commit comments

Comments
 (0)