Skip to content

Commit

Permalink
chore: convert API tests to node test (#1498)
Browse files Browse the repository at this point in the history
Convert tests that call the Looker API server to use node test runner

Test files renamed from `*.spec.ts` to `*.apitest.ts`

Only files with `.apitest` use the node test runner
  • Loading branch information
jkaster authored Sep 11, 2024
1 parent 748621e commit d2670ae
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tssdk-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ jobs:
${{ github.workspace }}/.github/scripts/wait_for_looker.sh
- name: Run Integration Tests
run: yarn test:nodej
run: yarn test:nodeci

- name: Upload integration test results
if: ${{ always() }}
Expand Down
2 changes: 1 addition & 1 deletion bin/looker-resources-index/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"author": "Looker",
"license": "MIT",
"devDependencies": {
"@types/node": "22.5.1",
"@types/node": "22.5.4",
"typescript": "5.5.4"
}
}
1 change: 0 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ module.exports = {
transformIgnorePatterns: [excludeNodeModulesExcept.string],
testPathIgnorePatterns: ['packages/.*?/lib'],
testEnvironment: require.resolve('jest-environment-jsdom'),
// testEnvironment: 'node',
testEnvironmentOptions: {
url: 'http://localhost/',
},
Expand Down
1 change: 0 additions & 1 deletion jest.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ const ResizeObserver = require('resize-observer-polyfill');
require('@testing-library/jest-dom');
require('jest-canvas-mock');
require('jest-styled-components');
// require('jest-environment-node');
require('jest-environment-jsdom');

configure({ adapter: new Adapter() });
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"dev:playground": "yarn workspace @looker/extension-playground develop",
"dev:tile": "yarn workspace @looker/extension-tile-playground develop",
"clean": "rm -Rf packages/*/lib",
"prepublishOnly": "jest packages/sdk-rtl packages/sdk-node/test",
"prepublishOnly": "jest packages/sdk-rtl",
"fix": "yarn lint:es --fix",
"lint": "run-p -c lint:*",
"lint:es": "eslint 'packages/**/*.ts{,x}' --cache",
Expand All @@ -70,8 +70,8 @@
"test:gen": "yarn jest packages/sdk-codegen",
"test:redux": "yarn jest packages/redux",
"test:sdk": "yarn jest packages/sdk",
"test:node": "find . -name \"*.test.ts\" -not -path \"./node_modules/*\" -exec node --import tsx --test-timeout 90000 --test {} ';'",
"test:nodej": "find . -name \"*.test.ts\" -not -path \"./node_modules/*\" -exec node --import tsx --test-timeout 90000 --test-reporter junit --test {} ';'",
"test:node": "find . -name \"*.apitest.ts\" -not -path \"./node_modules/*\" -exec node --import tsx --test-timeout 90000 --test {} ';'",
"test:nodeci": "find . -name \"sdk.apitest.ts\" -not -path \"./node_modules/*\" -exec node --import tsx --test-timeout 90000 --test-reporter junit --test {} ';'",
"test:jest": "DOT_ENV_FILE=.env.test jest",
"test:ext": "yarn jest packages/extension-sdk packages/extension-sdk-react",
"test:hack": "yarn jest packages/wholly-artifact/src packages/hackathon",
Expand Down Expand Up @@ -102,7 +102,7 @@
"@types/jest": "29.2.4",
"@types/js-yaml": "^3.12.1",
"@types/lodash": "4.14.172",
"@types/node": "22.5.1",
"@types/node": "22.5.4",
"@types/uuid": "^8.3.4",
"@typescript-eslint/eslint-plugin": "^5.61.0",
"@typescript-eslint/parser": "^5.61.0",
Expand Down
1 change: 1 addition & 0 deletions packages/sdk-codegen-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@openapitools/openapi-generator-cli": "^2.1.23",
"@types/config": "^0.0.36",
"dotenv": "^8.2.0",
"expect": "29.7.0",
"file-type": "^16.5.4",
"ini": "^1.3.8",
"js-yaml": "3.14.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
*/

import { readFileSync } from 'fs';
import { describe, it } from 'node:test';
import { expect } from 'expect';
import cloneDeep from 'lodash/cloneDeep';
import isEmpty from 'lodash/isEmpty';
import { LookerNodeSDK, NodeSettingsIniFile } from '@looker/sdk-node';
Expand Down Expand Up @@ -419,8 +421,7 @@ const settings = new NodeSettingsIniFile(
const sdk = LookerNodeSDK.init40(settings);
const baseUrl = sdk.authSession.settings.base_url;

// TODO get real fetch working for command-line Jest like it does in IntelliJ and VS Code
describe.skip('spec conversion', () => {
describe('spec conversion', () => {
it('swaps out x-looker-tags', () => {
const actual = swapXLookerTags(specFrag);
expect(actual).toContain('"nullable": true');
Expand Down Expand Up @@ -584,12 +585,12 @@ describe.skip('spec conversion', () => {
api_server_url: 'http://localhost:19999',
};

test('only gets supported specifications', async () => {
it('only gets supported specifications', async () => {
const actual = await getSpecsFromVersions(versions);
expect(Object.keys(actual)).toEqual(['3.1', '4.0']);
});

test('current is the default spec', async () => {
it('current is the default spec', async () => {
const specs = await getSpecsFromVersions(versions);
const actual = Object.entries(specs).find(
([_, a]) => a.status === 'current'
Expand All @@ -603,7 +604,7 @@ describe.skip('spec conversion', () => {
}
});

test('specs have unique keys', async () => {
it('specs have unique keys', async () => {
const moar = cloneDeep(versions);
moar.supported_versions.push(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
*/

import fs from 'fs';
import { describe, it } from 'node:test';
import { expect } from 'expect';
import { getSpecsFromVersions, specToModel } from '@looker/sdk-codegen';
import { TestConfig } from '@looker/sdk-codegen-utils';
import {
Expand All @@ -45,16 +47,15 @@ const props = ApiConfigSection(
// api_version is no longer part of the INI, now set by sdkGen iterator
props.api_version = '4.0';

// TODO get real fetch working for command-line Jest like it does in IntelliJ and VS Code
describe.skip('fetch operations', () => {
describe('fetch operations', () => {
it('defaults lookerVersion when server is not responding', async () => {
const testProps = JSON.parse(JSON.stringify(props));
testProps.base_url = 'https://bogus-server.looker.com:99';
const actual = await fetchLookerVersion(testProps, undefined, {
timeout: 3,
});
expect(actual).toEqual('');
}, 36000);
});

it('gets lookerVersion with good server', async () => {
const actual = await fetchLookerVersion(props);
Expand Down
6 changes: 3 additions & 3 deletions packages/sdk-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
},
"homepage": "https://github.com/looker-open-source/sdk-codegen/tree/main/packages/sdk-node",
"devDependencies": {
"@types/ini": "^1.3.30",
"@types/node": "22.5.1",
"@types/ini": "4.1.1",
"@types/node": "22.5.4",
"dotenv": "^8.2.0",
"expect": "29.7.0",
"whatwg-fetch": "3.6.20",
Expand All @@ -41,7 +41,7 @@
"dependencies": {
"@looker/sdk": "24.16.0",
"@looker/sdk-rtl": "21.6.2",
"ini": "^1.3.8"
"ini": "5.0.0"
},
"keywords": [
"Looker",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@
*/

import * as fs from 'fs';
// import 'whatwg-fetch';
import * as process from 'node:process';
import { describe, it } from 'node:test';
import { expect } from 'expect';
import { ApiConfigMap, boolDefault, defaultTimeout } from '@looker/sdk-rtl';
import { TestConfig } from '@looker/sdk-codegen-utils';
import { TestConfig } from '@looker/sdk-rtl/src/testUtils';
import { NodeTransport } from './nodeTransport';
import { NodeSession } from './nodeSession';
import { ApiConfig, NodeSettings, NodeSettingsIniFile } from './nodeSettings';
import { specToModel } from '@looker/sdk-codegen';

const config = TestConfig(specToModel);
const config = TestConfig();
const envPrefix = 'LOOKERSDK';
const localIni = config.localIni;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*/

import { describe, it } from 'node:test';
import { expect } from 'expect';
import type {
IRawResponse,
ISDKError,
Expand Down Expand Up @@ -68,7 +70,7 @@ describe('NodeTransport', () => {
});
});

test('retrieves fully qualified url', async () => {
it('retrieves fully qualified url', async () => {
const response = await xp.request<string, Error>('GET', fullPath);
expect(response).toBeDefined();
expect(response.ok).toEqual(true);
Expand Down Expand Up @@ -168,15 +170,15 @@ describe('NodeTransport', () => {
expect((typed as any).num4).toBe(4);
});
describe('NodeCryptoHash', () => {
test('secureRandom', () => {
it('secureRandom', () => {
const hasher = new NodeCryptoHash();
const rand1 = hasher.secureRandom(5);
expect(rand1.length).toEqual(10);
const rand2 = hasher.secureRandom(32);
expect(rand2.length).toEqual(64);
});

test('sha256hash', async () => {
it('sha256hash', async () => {
const hasher = new NodeCryptoHash();
const message = 'The quick brown fox jumped over the lazy dog.';
const hash = await hasher.sha256Hash(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/

import * as process from 'node:process';
import { before, beforeEach, after, afterEach, describe, it } from 'node:test';
import { createWritableStream, LookerNodeSDK, readIniConfig } from '../src';
import { expect } from 'expect';
Expand Down Expand Up @@ -57,10 +58,6 @@ import { TestConfig } from '@looker/sdk-codegen-utils';
import { specToModel } from '@looker/sdk-codegen';
import fs from 'fs';

// TODO Temporary equivalents to ease porting
const beforeAll = before;
const afterAll = after;

/** Note, these tests are for the Node test runner because jest has trouble with
* real calls to node's native fetch functionality
*/
Expand Down Expand Up @@ -423,11 +420,11 @@ describe('LookerNodeSDK integration tests', () => {
});

describe('User CRUD-it checks', () => {
beforeAll(async () => {
before(async () => {
await removeTestUsers();
});

afterAll(async () => {
after(async () => {
await removeTestUsers();
});

Expand Down Expand Up @@ -477,7 +474,7 @@ describe('LookerNodeSDK integration tests', () => {
});

describe('User searches', () => {
beforeAll(async () => {
before(async () => {
await removeTestUsers();
await createTestUsers();
});
Expand Down Expand Up @@ -608,7 +605,8 @@ describe('LookerNodeSDK integration tests', () => {
let count = 0;
let actual: IDashboard[] = [];
const aggregate = (page: IDashboard[]) => {
console.log(`Page ${++count} has ${page.length} items`);
count++;
// console.log(`Page ${count} has ${page.length} items`);
actual = actual.concat(page);
return page;
};
Expand All @@ -631,7 +629,8 @@ describe('LookerNodeSDK integration tests', () => {
let count = 0;
let actual: IDashboard[] = [];
const aggregate = (page: IDashboard[]) => {
console.log(`Page ${++count} has ${page.length} items`);
count++;
// console.log(`Page ${count} has ${page.length} items`);
actual = actual.concat(page);
return page;
};
Expand Down Expand Up @@ -762,7 +761,7 @@ describe('LookerNodeSDK integration tests', () => {

describe('parses a query with no results', () => {
let query;
beforeAll(async () => {
before(async () => {
query = await sdk.ok(
sdk.create_query({
model: 'system__activity',
Expand Down Expand Up @@ -813,7 +812,7 @@ describe('LookerNodeSDK integration tests', () => {
return qhash[Object.keys(qhash)[0]].id;
};

beforeAll(async () => {
before(async () => {
// test dashboards are removed here, but not in top-level tear-down because
// we may want to view them after the test
await removeTestDashboards();
Expand Down Expand Up @@ -1013,7 +1012,7 @@ describe('LookerNodeSDK integration tests', () => {
});

describe('Node environment', () => {
beforeAll(() => {
before(() => {
const section = readIniConfig(
config.localIni,
environmentPrefix,
Expand All @@ -1029,7 +1028,7 @@ describe('LookerNodeSDK integration tests', () => {
process.env[strLookerVerifySsl] = verify_ssl.toString();
});

afterAll(() => {
after(() => {
// reset environment variables
delete process.env[strLookerTimeout];
delete process.env[strLookerClientId];
Expand Down
18 changes: 14 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3860,6 +3860,11 @@
dependencies:
"@types/node" "*"

"@types/ini@4.1.1":
version "4.1.1"
resolved "https://registry.yarnpkg.com/@types/ini/-/ini-4.1.1.tgz#6984664a8cc74c3348f4049d0bf2b1ab2d061ca3"
integrity sha512-MIyNUZipBTbyUNnhvuXJTY7B6qNI78meck9Jbv3wk0OgNwRyOOVEKDutAkOs1snB/tx0FafyR6/SN4Ps0hZPeg==

"@types/ini@^1.3.30":
version "1.3.31"
resolved "https://registry.yarnpkg.com/@types/ini/-/ini-1.3.31.tgz#c78541a187bd88d5c73e990711c9d85214800d1b"
Expand Down Expand Up @@ -3974,10 +3979,10 @@
resolved "https://registry.yarnpkg.com/@types/ms/-/ms-0.7.34.tgz#10964ba0dee6ac4cd462e2795b6bebd407303433"
integrity sha512-nG96G3Wp6acyAgJqGasjODb+acrI7KltPiRxzHPXnP3NgI28bpQDRv53olbqGXbfcgF5aiiHmO3xpwEpS5Ld9g==

"@types/node@*", "@types/node@22.5.1", "@types/node@>= 8":
version "22.5.1"
resolved "https://registry.yarnpkg.com/@types/node/-/node-22.5.1.tgz#de01dce265f6b99ed32b295962045d10b5b99560"
integrity sha512-KkHsxej0j9IW1KKOOAA/XBA0z08UFSrRQHErzEfA3Vgq57eXIMYboIlHJuYIfd+lwCQjtKqUu3UnmKbtUc9yRw==
"@types/node@*", "@types/node@22.5.4", "@types/node@>= 8":
version "22.5.4"
resolved "https://registry.yarnpkg.com/@types/node/-/node-22.5.4.tgz#83f7d1f65bc2ed223bdbf57c7884f1d5a4fa84e8"
integrity sha512-FDuKUJQm/ju9fT/SeX/6+gBzoPzlVCzfzmGkwKvRHQVxi4BntVbyIwf6a4Xn62mrvndLiml6z/UBXIdEVjQLXg==
dependencies:
undici-types "~6.19.2"

Expand Down Expand Up @@ -9867,6 +9872,11 @@ inherits@2.0.3:
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de"
integrity sha512-x00IRNXNy63jwGkJmzPigoySHbaqpNuzKbBOmzK+g2OdZpQ9w+sxCN+VSB3ja7IAge2OP2qpfxTjeNcyjmW1uw==

ini@5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/ini/-/ini-5.0.0.tgz#a7a4615339843d9a8ccc2d85c9d81cf93ffbc638"
integrity sha512-+N0ngpO3e7cRUWOJAS7qw0IZIVc6XPrW4MlFBdD066F2L4k1L6ker3hLqSq7iXxU5tgS4WGkIUElWn5vogAEnw==

ini@^1.3.2, ini@^1.3.4, ini@^1.3.8:
version "1.3.8"
resolved "https://registry.yarnpkg.com/ini/-/ini-1.3.8.tgz#a29da425b48806f34767a4efce397269af28432c"
Expand Down

0 comments on commit d2670ae

Please sign in to comment.