Skip to content

Commit

Permalink
test: add mocks for packageVersion (#811)
Browse files Browse the repository at this point in the history
* test: add mock for packageInfo to list command

* test: add mock for packageInfo to TS generation tests

* chore: remove outdated comment
  • Loading branch information
kanadgupta authored Nov 2, 2023
1 parent 17f994a commit 590f372
Show file tree
Hide file tree
Showing 21 changed files with 42 additions and 36 deletions.
33 changes: 17 additions & 16 deletions packages/api/test/codegen/languages/typescript/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { SpyInstance } from 'vitest';

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

Expand Down Expand Up @@ -70,22 +72,16 @@ function assertSDKFixture(file: string, fixture: string) {
const actual = actualFiles[filename];
const expectedFilePath = path.join(dir, filename);

// We have to wrap in our current package version into the `<<useragent>>` placeholder so
// we don't need to worry about committing package versions into source control or trying
// to mock out our `packageInfo` library, potentially causing sideeffects in other tests.
return fs
.readFile(expectedFilePath, 'utf8')
.then(expected => expected.replace('<<package version>>', packageInfo.PACKAGE_VERSION))
.then(async expected => {
if (actual !== expected && process.env.UPDATE_FIXTURES) {
// eslint-disable-next-line no-console
console.info('[sdk fixture updated]', expectedFilePath);
await fs.writeFile(expectedFilePath, actual.replace(packageInfo.PACKAGE_VERSION, '<<package version>>'));
return;
}

expect(actual).toBe(expected);
});
return fs.readFile(expectedFilePath, 'utf8').then(async expected => {
if (actual !== expected && process.env.UPDATE_FIXTURES) {
// eslint-disable-next-line no-console
console.info('[sdk fixture updated]', expectedFilePath);
await fs.writeFile(expectedFilePath, actual);
return;
}

expect(actual).toBe(expected);
});
}),
);

Expand All @@ -96,12 +92,17 @@ function assertSDKFixture(file: string, fixture: string) {
}

describe('typescript', () => {
let packageInfoSpy: SpyInstance;

beforeEach(() => {
// @ts-expect-error deliberately setting this const to another value
packageInfoSpy = vi.spyOn(packageInfo, 'PACKAGE_VERSION', 'get').mockReturnValue('7.0.0-mock');
vi.setSystemTime(new Date('2023-10-25'));
Storage.setStorageDir(uniqueTempDir());
});

afterEach(() => {
packageInfoSpy.mockReset();
vi.useRealTimers();
Storage.reset();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/api/test/commands/__snapshots__/list.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ language: js
source: @petstore/v1.0#n6kvf10vakpemvplx
installer version: [90m7.0.0-beta.3[39m
installer version: [90m7.0.0-mock[39m
created at: 2023-10-25T00:00:00.000Z"
`;
Expand Down
7 changes: 6 additions & 1 deletion packages/api/test/commands/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { describe, beforeEach, it, expect, vi, afterEach } from 'vitest';

import { SupportedLanguages } from '../../src/codegen/factory.js';
import installCmd from '../../src/commands/list.js';
import * as packageInfo from '../../src/packageInfo.js';
import Storage from '../../src/storage.js';

const baseCommand = ['api', 'list'];
Expand All @@ -18,6 +19,7 @@ describe('install command', () => {
let stdout: string[];
let stderr: string[];
let consoleLogSpy: SpyInstance;
let packageInfoSpy: SpyInstance;

const getCommandOutput = () => {
return [consoleLogSpy.mock.calls.join('\n\n')].filter(Boolean).join('\n\n');
Expand All @@ -34,13 +36,16 @@ describe('install command', () => {
});

consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
// @ts-expect-error deliberately setting this const to another value
packageInfoSpy = vi.spyOn(packageInfo, 'PACKAGE_VERSION', 'get').mockReturnValue('7.0.0-mock');
Storage.setStorageDir(uniqueTempDir());
vi.setSystemTime(new Date('2023-10-25'));
});

afterEach(() => {
consoleLogSpy.mockRestore();
consoleLogSpy.mockReset();
fetchMock.restore();
packageInfoSpy.mockReset();
Storage.reset();
vi.useRealTimers();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/alby/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ alby.getMe()
Here's some additional info about the generated SDK:
`api` version: <<package version>>
`api` version: 7.0.0-mock
Generated at 2023-10-25T00:00:00.000Z
--->
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/alby/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SDK {
core: APICore;

constructor() {
this.core = new APICore(definition, 'alby/1.0.14 (api/<<package version>>)');
this.core = new APICore(definition, 'alby/1.0.14 (api/7.0.0-mock)');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/metrotransit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ metrotransit.getNextripAgencies()
Here's some additional info about the generated SDK:
`api` version: <<package version>>
`api` version: 7.0.0-mock
Generated at 2023-10-25T00:00:00.000Z
--->
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/metrotransit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SDK {
core: APICore;

constructor() {
this.core = new APICore(definition, 'metrotransit/2 (api/<<package version>>)');
this.core = new APICore(definition, 'metrotransit/2 (api/7.0.0-mock)');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/operationid-quirks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ operationidQuirks.quirky_OperationId_string()
Here's some additional info about the generated SDK:
`api` version: <<package version>>
`api` version: 7.0.0-mock
Generated at 2023-10-25T00:00:00.000Z
--->
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/operationid-quirks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class SDK {
core: APICore;

constructor() {
this.core = new APICore(definition, 'operationid-quirks/1.0.0 (api/<<package version>>)');
this.core = new APICore(definition, 'operationid-quirks/1.0.0 (api/7.0.0-mock)');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/optional-payload/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Add SDK setup information and usage examples here so your users get started in a
Here's some additional info about the generated SDK:
`api` version: <<package version>>
`api` version: 7.0.0-mock
Generated at 2023-10-25T00:00:00.000Z
--->
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/optional-payload/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SDK {
core: APICore;

constructor() {
this.core = new APICore(definition, 'optional-payload/1.0.0 (api/<<package version>>)');
this.core = new APICore(definition, 'optional-payload/1.0.0 (api/7.0.0-mock)');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/petstore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ petstore.getInventory()
Here's some additional info about the generated SDK:
`api` version: <<package version>>
`api` version: 7.0.0-mock
Generated at 2023-10-25T00:00:00.000Z
--->
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/petstore/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SDK {
core: APICore;

constructor() {
this.core = new APICore(definition, 'petstore/1.0.0 (api/<<package version>>)');
this.core = new APICore(definition, 'petstore/1.0.0 (api/7.0.0-mock)');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/readme/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ readme.getAPISpecification()
Here's some additional info about the generated SDK:
`api` version: <<package version>>
`api` version: 7.0.0-mock
Generated at 2023-10-25T00:00:00.000Z
--->
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/readme/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SDK {
core: APICore;

constructor() {
this.core = new APICore(definition, 'readme/4.355.0 (api/<<package version>>)');
this.core = new APICore(definition, 'readme/4.355.0 (api/7.0.0-mock)');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/response-title-quirks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Add SDK setup information and usage examples here so your users get started in a
Here's some additional info about the generated SDK:
`api` version: <<package version>>
`api` version: 7.0.0-mock
Generated at 2023-10-25T00:00:00.000Z
--->
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SDK {
core: APICore;

constructor() {
this.core = new APICore(definition, 'response-title-quirks/1.0.0 (api/<<package version>>)');
this.core = new APICore(definition, 'response-title-quirks/1.0.0 (api/7.0.0-mock)');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/simple/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Add SDK setup information and usage examples here so your users get started in a
Here's some additional info about the generated SDK:
`api` version: <<package version>>
`api` version: 7.0.0-mock
Generated at 2023-10-25T00:00:00.000Z
--->
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/simple/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SDK {
core: APICore;

constructor() {
this.core = new APICore(definition, 'simple/1.0.0 (api/<<package version>>)');
this.core = new APICore(definition, 'simple/1.0.0 (api/7.0.0-mock)');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/star-trek/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ starTrek.getAnimalSearch()
Here's some additional info about the generated SDK:
`api` version: <<package version>>
`api` version: 7.0.0-mock
Generated at 2023-10-25T00:00:00.000Z
--->
2 changes: 1 addition & 1 deletion packages/test-utils/sdks/star-trek/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SDK {
core: APICore;

constructor() {
this.core = new APICore(definition, 'star-trek/1.0.0 (api/<<package version>>)');
this.core = new APICore(definition, 'star-trek/1.0.0 (api/7.0.0-mock)');
}

/**
Expand Down

0 comments on commit 590f372

Please sign in to comment.