diff --git a/packages/api/test/__fixtures__/sdk/alby/index.ts b/packages/api/test/__fixtures__/sdk/alby/index.ts index 03ad90aa..8723065c 100644 --- a/packages/api/test/__fixtures__/sdk/alby/index.ts +++ b/packages/api/test/__fixtures__/sdk/alby/index.ts @@ -10,7 +10,7 @@ class SDK { constructor() { this.spec = Oas.init(definition); - this.core = new APICore(this.spec, 'alby/1.0.14 (api/5.0-unit-testing)'); + this.core = new APICore(this.spec, 'alby/1.0.14 (api/<>)'); } /** diff --git a/packages/api/test/__fixtures__/sdk/operationid-quirks/index.ts b/packages/api/test/__fixtures__/sdk/operationid-quirks/index.ts index 2cca5b8c..11ae2465 100644 --- a/packages/api/test/__fixtures__/sdk/operationid-quirks/index.ts +++ b/packages/api/test/__fixtures__/sdk/operationid-quirks/index.ts @@ -9,7 +9,7 @@ class SDK { constructor() { this.spec = Oas.init(definition); - this.core = new APICore(this.spec, 'operationid-quirks/1.0.0 (api/5.0-unit-testing)'); + this.core = new APICore(this.spec, 'operationid-quirks/1.0.0 (api/<>)'); } /** diff --git a/packages/api/test/__fixtures__/sdk/optional-payload/index.ts b/packages/api/test/__fixtures__/sdk/optional-payload/index.ts index 0374a840..5e035233 100644 --- a/packages/api/test/__fixtures__/sdk/optional-payload/index.ts +++ b/packages/api/test/__fixtures__/sdk/optional-payload/index.ts @@ -10,7 +10,7 @@ class SDK { constructor() { this.spec = Oas.init(definition); - this.core = new APICore(this.spec, 'optional-payload/1.0.0 (api/5.0-unit-testing)'); + this.core = new APICore(this.spec, 'optional-payload/1.0.0 (api/<>)'); } /** diff --git a/packages/api/test/__fixtures__/sdk/petstore/index.ts b/packages/api/test/__fixtures__/sdk/petstore/index.ts index e93dce89..2d1bd98b 100644 --- a/packages/api/test/__fixtures__/sdk/petstore/index.ts +++ b/packages/api/test/__fixtures__/sdk/petstore/index.ts @@ -10,7 +10,7 @@ class SDK { constructor() { this.spec = Oas.init(definition); - this.core = new APICore(this.spec, 'petstore/1.0.0 (api/5.0-unit-testing)'); + this.core = new APICore(this.spec, 'petstore/1.0.0 (api/<>)'); } /** diff --git a/packages/api/test/__fixtures__/sdk/readme/index.ts b/packages/api/test/__fixtures__/sdk/readme/index.ts index 2bdbe288..0d391ad6 100644 --- a/packages/api/test/__fixtures__/sdk/readme/index.ts +++ b/packages/api/test/__fixtures__/sdk/readme/index.ts @@ -10,7 +10,7 @@ class SDK { constructor() { this.spec = Oas.init(definition); - this.core = new APICore(this.spec, 'readme/2.0.0 (api/5.0-unit-testing)'); + this.core = new APICore(this.spec, 'readme/2.0.0 (api/<>)'); } /** diff --git a/packages/api/test/__fixtures__/sdk/response-title-quirks/index.ts b/packages/api/test/__fixtures__/sdk/response-title-quirks/index.ts index 398ace02..986d235e 100644 --- a/packages/api/test/__fixtures__/sdk/response-title-quirks/index.ts +++ b/packages/api/test/__fixtures__/sdk/response-title-quirks/index.ts @@ -10,7 +10,7 @@ class SDK { constructor() { this.spec = Oas.init(definition); - this.core = new APICore(this.spec, 'response-title-quirks/1.0.0 (api/5.0-unit-testing)'); + this.core = new APICore(this.spec, 'response-title-quirks/1.0.0 (api/<>)'); } /** diff --git a/packages/api/test/__fixtures__/sdk/simple-js-cjs/index.js b/packages/api/test/__fixtures__/sdk/simple-js-cjs/index.js index 41468649..2cbbf0d7 100644 --- a/packages/api/test/__fixtures__/sdk/simple-js-cjs/index.js +++ b/packages/api/test/__fixtures__/sdk/simple-js-cjs/index.js @@ -10,7 +10,7 @@ var simple_json_1 = __importDefault(require('../../../__fixtures__/definitions/s var SDK = /** @class */ (function () { function SDK() { this.spec = oas_1.default.init(simple_json_1.default); - this.core = new core_1.default(this.spec, 'simple-js-cjs/1.0.0 (api/5.0-unit-testing)'); + this.core = new core_1.default(this.spec, 'simple-js-cjs/1.0.0 (api/<>)'); } /** * Optionally configure various options that the SDK allows. diff --git a/packages/api/test/__fixtures__/sdk/simple-js-esm/index.js b/packages/api/test/__fixtures__/sdk/simple-js-esm/index.js index 11aff420..578cf60d 100644 --- a/packages/api/test/__fixtures__/sdk/simple-js-esm/index.js +++ b/packages/api/test/__fixtures__/sdk/simple-js-esm/index.js @@ -4,7 +4,7 @@ import definition from '../../../__fixtures__/definitions/simple.json'; class SDK { constructor() { this.spec = Oas.init(definition); - this.core = new APICore(this.spec, 'simple-js-esm/1.0.0 (api/5.0-unit-testing)'); + this.core = new APICore(this.spec, 'simple-js-esm/1.0.0 (api/<>)'); } /** * Optionally configure various options that the SDK allows. diff --git a/packages/api/test/__fixtures__/sdk/simple-ts/index.ts b/packages/api/test/__fixtures__/sdk/simple-ts/index.ts index b68e4dc2..9b300da7 100644 --- a/packages/api/test/__fixtures__/sdk/simple-ts/index.ts +++ b/packages/api/test/__fixtures__/sdk/simple-ts/index.ts @@ -10,7 +10,7 @@ class SDK { constructor() { this.spec = Oas.init(definition); - this.core = new APICore(this.spec, 'simple-ts/1.0.0 (api/5.0-unit-testing)'); + this.core = new APICore(this.spec, 'simple-ts/1.0.0 (api/<>)'); } /** diff --git a/packages/api/test/cli/codegen/languages/typescript.test.ts b/packages/api/test/cli/codegen/languages/typescript.test.ts index c0fed878..9316d4a8 100644 --- a/packages/api/test/cli/codegen/languages/typescript.test.ts +++ b/packages/api/test/cli/codegen/languages/typescript.test.ts @@ -1,4 +1,3 @@ -/* eslint-disable import/first */ import type { TSGeneratorOptions } from '../../../../src/cli/codegen/languages/typescript'; import { promises as fs } from 'fs'; @@ -6,22 +5,11 @@ import path from 'path'; import chai, { assert, expect } from 'chai'; import fetchMock from 'fetch-mock'; -import mockRequire from 'mock-require'; import Oas from 'oas'; import sinon from 'sinon'; import sinonChai from 'sinon-chai'; import uniqueTempDir from 'unique-temp-dir'; -// Because `packageInfo` is generated at build time and we're using it in tests, everytime we build -// a new release our tests will also need to get updated because we use `PACKAGE_VERSION` in user -// agent strings. Because nobody wants to have to fix broken tests everytime we build a new release -// we need to overload the `packageInfo` import here with some fake data that we can then use in our -// test snapshots. -mockRequire('../../../../src/packageInfo', { - PACKAGE_NAME: 'api', - PACKAGE_VERSION: '5.0-unit-testing', -}); - import TSGenerator from '../../../../src/cli/codegen/languages/typescript'; import Storage from '../../../../src/cli/storage'; import chaiPlugins from '../../../helpers/chai-plugins'; diff --git a/packages/api/test/helpers/chai-plugins.ts b/packages/api/test/helpers/chai-plugins.ts index 1b49c533..69a362b5 100644 --- a/packages/api/test/helpers/chai-plugins.ts +++ b/packages/api/test/helpers/chai-plugins.ts @@ -5,6 +5,8 @@ import path from 'path'; import caseless from 'caseless'; import chai from 'chai'; +import * as packageInfo from '../../src/packageInfo'; + declare global { // eslint-disable-next-line @typescript-eslint/no-namespace namespace Chai { @@ -40,7 +42,7 @@ export default function chaiPlugins(_chai, utils) { const userAgent = this._obj['user-agent']; this.assert( - userAgent.match(/api \(node\)\/\d+.\d+.\d+/), + userAgent.match(/^api \(node\)\/(\d+.\d+(.\d+|unit-testing))$/), `expected "${userAgent}" to be a custom user agent`, `expected "${userAgent}" to not be a custom user agent` ); @@ -77,8 +79,16 @@ export default function chaiPlugins(_chai, utils) { // Assert that each generated file is the same as in the fixture. expectedFiles.forEach(file => { - const expected = fs.readFileSync(path.join(dir, file), 'utf8'); - new chai.Assertion(actualFiles[file], `${file} does not match`).to.equal(expected); + const actual = actualFiles[file]; + + // We have to wrap in our current package version into the `<>` 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. + const expected = fs + .readFileSync(path.join(dir, file), 'utf8') + .replace('<>', packageInfo.PACKAGE_VERSION); + + new chai.Assertion(actual, `${file} does not match`).to.equal(expected); }); }); diff --git a/packages/api/test/integration.test.ts b/packages/api/test/integration.test.ts index 538c3a32..e22834bd 100644 --- a/packages/api/test/integration.test.ts +++ b/packages/api/test/integration.test.ts @@ -62,7 +62,7 @@ describe('integration tests', function () { expect(data.uri).to.equal('/anything/v1/oa_citations/records'); expect(data.requestBody).to.equal('criteria=propertyName%3Avalue'); expect(data.headers).to.have.deep.property('content-type', 'application/x-www-form-urlencoded'); - expect(data.headers).to.have.a.customUserAgent; + expect(data.headers).to.have.be.a.customUserAgent; }); // it.skip('should send along required parameters if not supplied', async function () {