Skip to content

Commit

Permalink
chore: convert API tests to node test
Browse files Browse the repository at this point in the history
  • Loading branch information
jkaster committed Sep 11, 2024
1 parent bd34fa8 commit cdcf733
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 30 deletions.
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:nodej": "find . -name \"*.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
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
1 change: 1 addition & 0 deletions packages/sdk-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"devDependencies": {
"@types/ini": "^1.3.30",
"@types/node": "22.5.1",
"@looker/sdk-codegen": "21.9.0",
"dotenv": "^8.2.0",
"expect": "29.7.0",
"whatwg-fetch": "3.6.20",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@
*/

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 { specToModel } from '@looker/sdk-codegen';
import { ApiConfigMap, boolDefault, defaultTimeout } from '@looker/sdk-rtl';
import { TestConfig } from '@looker/sdk-codegen-utils';
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 envPrefix = 'LOOKERSDK';
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

0 comments on commit cdcf733

Please sign in to comment.