Skip to content

Commit

Permalink
chore: Standardize test organization.
Browse files Browse the repository at this point in the history
  • Loading branch information
kinyoklion committed Sep 4, 2024
1 parent fcb821e commit a52014b
Show file tree
Hide file tree
Showing 53 changed files with 92 additions and 86 deletions.
12 changes: 9 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,20 @@ To install project dependencies, from the project root directory:
yarn
```

### Testing
### Build

To run all unit tests:
To build all projects, from the root directory:

```
yarn test
yarn build
```

### Testing

Unit tests should be implemented in a `__tests__` folder in the root of the package. The directory structure inside of `__tests__` should mirror that of the source directory.

Each package has its own testing requirements and tests should be only ran for single projects.

To run the SDK contract test suite (see [`contract-tests/README.md`](./contract-tests/README.md)):
The SDK contract test suite will run the Node.js Server version of the SDK.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EdgeProvider, init } from '../index';
import { EdgeProvider, init } from '../src/index';
import * as testData from './testData.json';

const sdkKey = 'test-sdk-key';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EdgeKV } from '../../edgekv/edgekv';
import EdgeKVProvider from '../../edgekv/edgeKVProvider';
import { EdgeKV } from '../../src/edgekv/edgekv';
import EdgeKVProvider from '../../src/edgekv/edgeKVProvider';

jest.mock('../../edgekv/edgekv', () => ({
jest.mock('../../src/edgekv/edgekv', () => ({
EdgeKV: jest.fn(),
}));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import EdgeKVProvider from '../edgekv/edgeKVProvider';
import { init as initWithEdgeKV, LDClient, LDContext } from '../index';
import EdgeKVProvider from '../src/edgekv/edgeKVProvider';
import { init as initWithEdgeKV, LDClient, LDContext } from '../src/index';
import * as testData from './testData.json';

jest.mock('../edgekv/edgekv', () => ({
jest.mock('../src/edgekv/edgekv', () => ({
EdgeKV: jest.fn(),
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Miniflare } from 'miniflare';

import { LDClient, LDContext } from '@launchdarkly/js-server-sdk-common-edge';

import { init } from './index';
import { init } from '../src/index';
import * as allFlagsSegments from './testData.json';

const mf = new Miniflare({
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AutoEnvAttributes, LDLogger } from '@launchdarkly/js-client-sdk-common';

import ReactNativeLDClient from './ReactNativeLDClient';
import ReactNativeLDClient from '../src/ReactNativeLDClient';

it('uses custom storage', async () => {
// This test just validates that the custom storage instance is being called.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { AutoEnvAttributes, LDLogger, Response } from '@launchdarkly/js-client-sdk-common';

import createPlatform from './platform';
import PlatformCrypto from './platform/crypto';
import PlatformEncoding from './platform/PlatformEncoding';
import PlatformInfo from './platform/PlatformInfo';
import PlatformStorage from './platform/PlatformStorage';
import ReactNativeLDClient from './ReactNativeLDClient';
import createPlatform from '../src/platform';
import PlatformCrypto from '../src/platform/crypto';
import PlatformEncoding from '../src/platform/PlatformEncoding';
import PlatformInfo from '../src/platform/PlatformInfo';
import PlatformStorage from '../src/platform/PlatformStorage';
import ReactNativeLDClient from '../src/ReactNativeLDClient';

function mockResponse(value: string, statusCode: number) {
const response: Response = {
Expand Down Expand Up @@ -34,7 +34,7 @@ function mockFetch(value: string, statusCode: number = 200) {
return f;
}

jest.mock('./platform', () => ({
jest.mock('../src/platform', () => ({
__esModule: true,
default: jest.fn((logger: LDLogger) => ({
crypto: new PlatformCrypto(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type EventName } from '@launchdarkly/js-client-sdk-common';
import { createLogger } from '@launchdarkly/private-js-mocks';

import EventSource, { backoff, jitter } from './EventSource';
import EventSource, { backoff, jitter } from '../../../src/fromExternal/react-native-sse/EventSource';

let logger: ReturnType<typeof createLogger>;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { LDLogger } from '@launchdarkly/js-client-sdk-common';

import validateOptions, { filterToBaseOptions } from './options';
import { RNStorage } from './RNOptions';
import validateOptions, { filterToBaseOptions } from '../src/options';
import { RNStorage } from '../src/RNOptions';

it('logs no warnings when all configuration is valid', () => {
const logger: LDLogger = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ConnectionManager,
NetworkState,
StateDetector,
} from './ConnectionManager';
} from '../../src/platform/ConnectionManager';

function mockDestination(): ConnectionDestination {
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import PlatformHasher from './PlatformHasher';
import PlatformHasher from '../../../src/platform/crypto/PlatformHasher';

/**
* The links below are different from js-sha256 and are useful to verify the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { render } from '@testing-library/react';

import { AutoEnvAttributes, LDContext, LDOptions } from '@launchdarkly/js-client-sdk-common';

import { useLDClient } from '../hooks';
import ReactNativeLDClient from '../ReactNativeLDClient';
import LDProvider from './LDProvider';
import setupListeners from './setupListeners';
import { useLDClient } from '../../src/hooks';
import ReactNativeLDClient from '../../src/ReactNativeLDClient';

Check failure on line 6 in packages/sdk/react-native/__tests__/provider/LDProvider.test.tsx

View workflow job for this annotation

GitHub Actions / build-test-react-native

Delete `ReactNativeLDClient·from·'../../src/ReactNativeLDClient';⏎import·`
import LDProvider from '../../src/provider/LDProvider';
import setupListeners from '../../src/provider/setupListeners';

Check failure on line 8 in packages/sdk/react-native/__tests__/provider/LDProvider.test.tsx

View workflow job for this annotation

GitHub Actions / build-test-react-native

Insert `';⏎import·ReactNativeLDClient·from·'../../src/ReactNativeLDClient`

jest.mock('../ReactNativeLDClient');
jest.mock('./setupListeners');
jest.mock('../../src/ReactNativeLDClient');
jest.mock('../../src/provider/setupListeners');

const TestApp = () => {
const ldClient = useLDClient();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { AutoEnvAttributes } from '@launchdarkly/js-client-sdk-common';

import ReactNativeLDClient from '../ReactNativeLDClient';
import setupListeners from './setupListeners';
import ReactNativeLDClient from '../../src/ReactNativeLDClient';

Check failure on line 3 in packages/sdk/react-native/__tests__/provider/setupListeners.test.ts

View workflow job for this annotation

GitHub Actions / build-test-react-native

Replace `ReactNativeLDClient·from·'../../src/ReactNativeLDClient` with `setupListeners·from·'../../src/provider/setupListeners`
import setupListeners from '../../src/provider/setupListeners';

Check failure on line 4 in packages/sdk/react-native/__tests__/provider/setupListeners.test.ts

View workflow job for this annotation

GitHub Actions / build-test-react-native

Replace `setupListeners·from·'../../src/provider/setupListeners` with `ReactNativeLDClient·from·'../../src/ReactNativeLDClient`

import resetAllMocks = jest.resetAllMocks;

jest.mock('../ReactNativeLDClient');
jest.mock('../../src/ReactNativeLDClient');

describe('setupListeners', () => {
let ldc: ReactNativeLDClient;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import createPlatformInfo from './createPlatformInfo';
import createPlatformInfo from '../src/createPlatformInfo';

const packageJson = require('../package.json');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LDClient, LDContext } from '@launchdarkly/js-server-sdk-common-edge';

import { init } from './index';
import { init } from '../src/index';
import mockEdgeConfigClient from './utils/mockEdgeConfigClient';
import * as testData from './utils/testData.json';

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AsyncStoreFacade, LDFeatureStore } from '@launchdarkly/js-server-sdk-common';

import { EdgeFeatureStore, EdgeProvider } from '../../featureStore';
import { EdgeFeatureStore, EdgeProvider } from '../../src/featureStore';
import * as testData from '../testData.json';

describe('EdgeFeatureStore', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EdgeProvider, init, LDLogger, LDMultiKindContext, LDSingleKindContext } from '../..';
import { EdgeProvider, init, LDLogger, LDMultiKindContext, LDSingleKindContext } from '../dist';
import * as testData from './testData.json';

const createClient = (sdkKey: string, mockLogger: LDLogger, mockEdgeProvider: EdgeProvider) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import createPlatformInfo from '../../../platform/info';
import createPlatformInfo from '../../../src/platform/info';

const packageJson = require('../../../../package.json');
const packageJson = require('../../../package.json');

describe('Akamai Platform Info', () => {
const { name, version } = packageJson;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EventSourceInitDict } from '@launchdarkly/js-server-sdk-common';

import EdgeRequests from '../../platform/requests';
import EdgeRequests from '../../src/platform/requests';

const TEXT_RESPONSE = '';
const JSON_RESPONSE = {};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createCallbacks } from '../../utils/createCallbacks';
import { createCallbacks } from '../../src/utils/createCallbacks';

describe('create callback', () => {
it('creates valid callbacks', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createOptions, defaultOptions } from '../../utils/createOptions';
import { createOptions, defaultOptions } from '../../src/utils/createOptions';

describe('create options', () => {
it('returns default options', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BasicLogger } from '@launchdarkly/js-server-sdk-common';

import { EdgeFeatureStore } from '../../featureStore';
import { LDOptionsInternal, validateOptions } from '../../utils/validateOptions';
import { EdgeFeatureStore } from '../../src/featureStore';
import { LDOptionsInternal, validateOptions } from '../../src/utils/validateOptions';

const SDK_KEY = 'test-key';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import {
} from '@launchdarkly/private-js-mocks';

import * as mockResponseJson from './evaluation/mockResponse.json';

Check failure on line 16 in packages/shared/sdk-client/__tests__/LDClientImpl.events.test.ts

View workflow job for this annotation

GitHub Actions / build-test-sdk-client

Delete `*·as·mockResponseJson·from·'./evaluation/mockResponse.json';⏎import·`
import LDClientImpl from './LDClientImpl';
import { Flags } from './types';
import LDClientImpl from '../src/LDClientImpl';
import { Flags } from '../src/types';

Check failure on line 18 in packages/shared/sdk-client/__tests__/LDClientImpl.events.test.ts

View workflow job for this annotation

GitHub Actions / build-test-sdk-client

Insert `';⏎import·*·as·mockResponseJson·from·'./evaluation/mockResponse.json`

type InputCustomEvent = internal.InputCustomEvent;
type InputIdentifyEvent = internal.InputIdentifyEvent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import {
setupMockStreamingProcessor,
} from '@launchdarkly/private-js-mocks';

import { toMulti } from './context/addAutoEnv';
import { toMulti } from '../src/context/addAutoEnv';
import * as mockResponseJson from './evaluation/mockResponse.json';

Check failure on line 9 in packages/shared/sdk-client/__tests__/LDClientImpl.storage.test.ts

View workflow job for this annotation

GitHub Actions / build-test-sdk-client

Delete `*·as·mockResponseJson·from·'./evaluation/mockResponse.json';⏎import·`
import LDClientImpl from './LDClientImpl';
import LDEmitter from './LDEmitter';
import { DeleteFlag, Flags, PatchFlag } from './types';
import LDClientImpl from '../src/LDClientImpl';
import LDEmitter from '../src/LDEmitter';
import { DeleteFlag, Flags, PatchFlag } from '../src/types';

Check failure on line 12 in packages/shared/sdk-client/__tests__/LDClientImpl.storage.test.ts

View workflow job for this annotation

GitHub Actions / build-test-sdk-client

Insert `';⏎import·*·as·mockResponseJson·from·'./evaluation/mockResponse.json`

let mockPlatform: ReturnType<typeof createBasicPlatform>;
let logger: ReturnType<typeof createLogger>;
Expand Down Expand Up @@ -186,7 +186,7 @@ describe('sdk-client storage', () => {
test('not emitting change event when changed keys is empty', async () => {
let LDClientImplTestNoChange;
jest.isolateModules(async () => {
LDClientImplTestNoChange = jest.requireActual('./LDClientImpl').default;
LDClientImplTestNoChange = jest.requireActual('../src/LDClientImpl').default;
ldc = new LDClientImplTestNoChange(testSdkKey, AutoEnvAttributes.Enabled, mockPlatform, {
logger,
sendEvents: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
} from '@launchdarkly/private-js-mocks';

import * as mockResponseJson from './evaluation/mockResponse.json';

Check failure on line 9 in packages/shared/sdk-client/__tests__/LDClientImpl.test.ts

View workflow job for this annotation

GitHub Actions / build-test-sdk-client

Delete `*·as·mockResponseJson·from·'./evaluation/mockResponse.json';⏎import·`
import LDClientImpl from './LDClientImpl';
import { Flags } from './types';
import LDClientImpl from '../src/LDClientImpl';
import { Flags } from '../src/types';

Check failure on line 11 in packages/shared/sdk-client/__tests__/LDClientImpl.test.ts

View workflow job for this annotation

GitHub Actions / build-test-sdk-client

Insert `';⏎import·*·as·mockResponseJson·from·'./evaluation/mockResponse.json`

jest.mock('@launchdarkly/js-sdk-common', () => {
const actual = jest.requireActual('@launchdarkly/js-sdk-common');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
setupMockStreamingProcessor,
} from '@launchdarkly/private-js-mocks';

import { toMulti } from './context/addAutoEnv';
import { toMulti } from '../src/context/addAutoEnv';
import * as mockResponseJson from './evaluation/mockResponse.json';

Check failure on line 9 in packages/shared/sdk-client/__tests__/LDClientImpl.timeout.test.ts

View workflow job for this annotation

GitHub Actions / build-test-sdk-client

Delete `*·as·mockResponseJson·from·'./evaluation/mockResponse.json';⏎import·`
import LDClientImpl from './LDClientImpl';
import { Flags } from './types';
import LDClientImpl from '../src/LDClientImpl';
import { Flags } from '../src/types';

Check failure on line 11 in packages/shared/sdk-client/__tests__/LDClientImpl.timeout.test.ts

View workflow job for this annotation

GitHub Actions / build-test-sdk-client

Insert `';⏎import·*·as·mockResponseJson·from·'./evaluation/mockResponse.json`

let mockPlatform: ReturnType<typeof createBasicPlatform>;
let logger: ReturnType<typeof createLogger>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
} from '@launchdarkly/private-js-mocks';

import * as mockResponseJson from './evaluation/mockResponse.json';

Check failure on line 8 in packages/shared/sdk-client/__tests__/LDClientImpl.variation.test.ts

View workflow job for this annotation

GitHub Actions / build-test-sdk-client

Delete `*·as·mockResponseJson·from·'./evaluation/mockResponse.json';⏎import·`
import LDClientImpl from './LDClientImpl';
import { Flags } from './types';
import LDClientImpl from '../src/LDClientImpl';
import { Flags } from '../src/types';

Check failure on line 10 in packages/shared/sdk-client/__tests__/LDClientImpl.variation.test.ts

View workflow job for this annotation

GitHub Actions / build-test-sdk-client

Insert `';⏎import·*·as·mockResponseJson·from·'./evaluation/mockResponse.json`

let mockPlatform: ReturnType<typeof createBasicPlatform>;
let logger: ReturnType<typeof createLogger>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LDContext, LDLogger } from '@launchdarkly/js-sdk-common';

import LDEmitter from './LDEmitter';
import LDEmitter from '../src/LDEmitter';

describe('LDEmitter', () => {
const error = { type: 'network', message: 'unreachable' };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-console */
import Configuration from './Configuration';
import Configuration from '../../src/configuration/Configuration';

describe('Configuration', () => {
beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
} from '@launchdarkly/js-sdk-common';
import { createBasicPlatform, createLogger } from '@launchdarkly/private-js-mocks';

import Configuration from '../configuration';
import { addApplicationInfo, addAutoEnv, addDeviceInfo, toMulti } from './addAutoEnv';
import Configuration from '../../src/configuration';
import { addApplicationInfo, addAutoEnv, addDeviceInfo, toMulti } from '../../src/context/addAutoEnv';

let mockPlatform: ReturnType<typeof createBasicPlatform>;
let logger: ReturnType<typeof createLogger>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
} from '@launchdarkly/js-sdk-common';
import { createBasicPlatform } from '@launchdarkly/private-js-mocks';

import { ensureKey } from './ensureKey';
import { ensureKey } from '../../src/context/ensureKey';

let mockPlatform: ReturnType<typeof createBasicPlatform>;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { secondsToMillis } from '@launchdarkly/js-sdk-common';

import Configuration from '../configuration';
import Configuration from '../../src/configuration';
import createDiagnosticsInitConfig, {
type DiagnosticsInitConfig,
} from './createDiagnosticsInitConfig';
} from '../../src/diagnostics/createDiagnosticsInitConfig';

describe('createDiagnosticsInitConfig', () => {
let initConfig: DiagnosticsInitConfig;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ContextIndex from './ContextIndex';
import ContextIndex from '../../src/flag-manager/ContextIndex';

describe('ContextIndex tests', () => {
test('notice adds to index', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* eslint-disable @typescript-eslint/no-use-before-define */
import { Context, Crypto, Hasher, LDLogger, Platform, Storage } from '@launchdarkly/js-sdk-common';

import { namespaceForContextData, namespaceForContextIndex } from '../storage/namespaceUtils';
import { Flag, Flags } from '../types';
import FlagPersistence from './FlagPersistence';
import { DefaultFlagStore } from './FlagStore';
import FlagUpdater from './FlagUpdater';
import { namespaceForContextData, namespaceForContextIndex } from '../../src/storage/namespaceUtils';
import { Flag, Flags } from '../../src/types';
import FlagPersistence from '../../src/flag-manager/FlagPersistence';
import { DefaultFlagStore } from '../../src/flag-manager/FlagStore';
import FlagUpdater from '../../src/flag-manager/FlagUpdater';

const TEST_NAMESPACE = 'TestNamespace';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Context, LDLogger } from '@launchdarkly/js-sdk-common';

import { Flag } from '../types';
import { DefaultFlagStore } from './FlagStore';
import FlagUpdater, { FlagsChangeCallback } from './FlagUpdater';
import { Flag } from '../../src/types';
import { DefaultFlagStore } from '../../src/flag-manager/FlagStore';
import FlagUpdater, { FlagsChangeCallback } from '../../src/flag-manager/FlagUpdater';

function makeMockFlag(): Flag {
// the values of the flag object itself are not relevant for these tests, the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
SdkData,
} from '@launchdarkly/js-sdk-common';

import PollingProcessor, { PollingConfig } from './PollingProcessor';
import PollingProcessor, { PollingConfig } from '../../src/polling/PollingProcessor';

function mockResponse(value: string, statusCode: number) {
const response: Response = {
Expand Down
Loading

0 comments on commit a52014b

Please sign in to comment.