Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }],
"class-methods-use-this": "off"
},
"overrides": [
{
"files": ["src/test/**"],
"rules": {
"import/no-extraneous-dependencies": ["error", { "devDependencies": true }],
"import/no-named-as-default-member": "off"
}
},
{
"files": ["vitest.config.ts"],
"rules": {
"import/no-extraneous-dependencies": ["error", { "devDependencies": true }]
}
}
],
"settings": {
"import/resolver": {
"typescript": true
Expand Down
2 changes: 1 addition & 1 deletion src/test/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-nocheck
import test from 'ava';
import test from './test-shim';
import Client from '../client';
import { defaultStrategies, Strategy } from '../strategy';

Expand Down
2 changes: 1 addition & 1 deletion src/test/global.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import test from 'ava';
import test from './test-shim';
import * as nock from 'nock';
import { tmpdir } from 'os';
import { join } from 'path';
Expand Down
2 changes: 1 addition & 1 deletion src/test/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import test from 'ava';
import test from './test-shim';

import { getAppliedJitter } from '../helpers';

Expand Down
2 changes: 1 addition & 1 deletion src/test/impact-metrics/environment-resolver.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import test from 'ava';
import test from '../test-shim';
import { extractEnvironmentFromCustomHeaders } from '../../impact-metrics/environment-resolver';

test('valid headers', (t) => {
Expand Down
2 changes: 1 addition & 1 deletion src/test/impact-metrics/metric-client.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MetricsAPI } from '../../impact-metrics/metric-api';

import test from 'ava';
import test from '../test-shim';
import Client from '../../client';
import { MetricLabels } from '../../impact-metrics/metric-types';

Expand Down
2 changes: 1 addition & 1 deletion src/test/impact-metrics/metric-types.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import test from 'ava';
import test from '../test-shim';
import { InMemoryMetricRegistry } from '../../impact-metrics/metric-types';

test('Counter increments by default value', (t) => {
Expand Down
2 changes: 1 addition & 1 deletion src/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import test from 'ava';
import test from './test-shim';
import * as nock from 'nock';
import {
initialize,
Expand Down
2 changes: 1 addition & 1 deletion src/test/integration/client-specification.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import test from 'ava';
import test from '../test-shim';
import * as nock from 'nock';
import { tmpdir } from 'os';
import { join } from 'path';
Expand Down
2 changes: 1 addition & 1 deletion src/test/integration/constraint.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import test from 'ava';
import test from '../test-shim';
import * as nock from 'nock';
import { tmpdir } from 'os';
import { join } from 'path';
Expand Down
2 changes: 1 addition & 1 deletion src/test/integration/context.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import test from 'ava';
import test from '../test-shim';
import * as nock from 'nock';
import { tmpdir } from 'os';
import { join } from 'path';
Expand Down
2 changes: 1 addition & 1 deletion src/test/integration/segment.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import test from 'ava';
import test from '../test-shim';
import * as nock from 'nock';
import { tmpdir } from 'os';
import { join } from 'path';
Expand Down
2 changes: 1 addition & 1 deletion src/test/metrics.network.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import test from 'ava';
import test from './test-shim';
import * as nock from 'nock';
import Metrics from '../metrics';

Expand Down
4 changes: 2 additions & 2 deletions src/test/metrics.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import test from 'ava';
import test from './test-shim';
import * as nock from 'nock';
import Metrics from '../metrics';
import type { CollectedMetric } from '../impact-metrics/metric-types';
Expand All @@ -21,7 +21,7 @@ test('should be disabled by flag disableMetrics', (t) => {
});

test('registerInstance, sendMetrics, startTimer and count should respect disabled', (t) =>
new Promise((resolve) => {
new Promise<void>((resolve) => {
const url = getUrl();
// @ts-expect-error
const metrics = new Metrics({
Expand Down
42 changes: 21 additions & 21 deletions src/test/repository/repository.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import test from 'ava';
import test from '../test-shim';
import * as nock from 'nock';
import { writeFileSync } from 'fs';
import { tmpdir } from 'os';
Expand Down Expand Up @@ -52,7 +52,7 @@ function createSSEResponse(events: Array<{ event: string; data: any }>) {
}

test('should fetch from endpoint', (t) =>
new Promise((resolve) => {
new Promise<void>((resolve) => {
const url = 'http://unleash-test-0.app';
const feature = {
name: 'feature',
Expand Down Expand Up @@ -90,7 +90,7 @@ test('should fetch from endpoint', (t) =>
}));

test('should poll for changes', (t) =>
new Promise((resolve, reject) => {
new Promise<void>((resolve, reject) => {
const url = 'http://unleash-test-2.app';
setup(url, []);
const repo = new Repository({
Expand Down Expand Up @@ -121,7 +121,7 @@ test('should poll for changes', (t) =>
}));

test('should retry even if custom header function fails', (t) =>
new Promise((resolve) => {
new Promise<void>((resolve) => {
const url = 'http://unleash-test-2-custom-headers.app';
setup(url, []);
const repo = new Repository({
Expand Down Expand Up @@ -153,7 +153,7 @@ test('should retry even if custom header function fails', (t) =>
}));

test('should store etag', (t) =>
new Promise((resolve) => {
new Promise<void>((resolve) => {
const url = 'http://unleash-test-3.app';
setup(url, [], { Etag: '12345' });
const repo = new Repository({
Expand All @@ -177,7 +177,7 @@ test('should store etag', (t) =>
}));

test('should request with etag', (t) =>
new Promise((resolve) => {
new Promise<void>((resolve) => {
const url = 'http://unleash-test-4.app';
nock(url)
.matchHeader('If-None-Match', '12345-1')
Expand Down Expand Up @@ -208,7 +208,7 @@ test('should request with etag', (t) =>
}));

test('should request with correct custom and unleash headers', (t) =>
new Promise((resolve) => {
new Promise<void>((resolve) => {
const url = 'http://unleash-test-4-x.app';
const randomKey = `random-${Math.random()}`;
nock(url)
Expand Down Expand Up @@ -249,7 +249,7 @@ test('should request with correct custom and unleash headers', (t) =>
}));

test('request with customHeadersFunction should take precedence over customHeaders', (t) =>
new Promise((resolve) => {
new Promise<void>((resolve) => {
const url = 'http://unleash-test-4-x.app';
const randomKey = `random-${Math.random()}`;
const customHeaderKey = `customer-${Math.random()}`;
Expand Down Expand Up @@ -346,7 +346,7 @@ test('should handle 401 request error and emit error event', (t) =>
}));

test('should handle 403 request error and emit error event', (t) =>
new Promise((resolve) => {
new Promise<void>((resolve) => {
const url = 'http://unleash-test-6-403.app';
nock(url).persist().get('/client/features').reply(403, 'blabla');
const repo = new Repository({
Expand Down Expand Up @@ -394,7 +394,7 @@ test('should handle 500 request error and emit warn event', (t) =>
repo.start();
}));
test.skip('should handle 502 request error and emit warn event', (t) =>
new Promise((resolve) => {
new Promise<void>((resolve) => {
const url = 'http://unleash-test-6-502.app';
nock(url).persist().get('/client/features').reply(502, 'blabla');
const repo = new Repository({
Expand All @@ -415,7 +415,7 @@ test.skip('should handle 502 request error and emit warn event', (t) =>
repo.start();
}));
test.skip('should handle 503 request error and emit warn event', (t) =>
new Promise((resolve) => {
new Promise<void>((resolve) => {
const url = 'http://unleash-test-6-503.app';
nock(url).persist().get('/client/features').reply(503, 'blabla');
const repo = new Repository({
Expand All @@ -436,7 +436,7 @@ test.skip('should handle 503 request error and emit warn event', (t) =>
repo.start();
}));
test.skip('should handle 504 request error and emit warn event', (t) =>
new Promise((resolve) => {
new Promise<void>((resolve) => {
const url = 'http://unleash-test-6-504.app';
nock(url).persist().get('/client/features').reply(504, 'blabla');
const repo = new Repository({
Expand Down Expand Up @@ -482,7 +482,7 @@ test('should handle 304 as silent ok', (t) => {
});

test('should handle invalid JSON response', (t) =>
new Promise((resolve, reject) => {
new Promise<void>((resolve, reject) => {
const url = 'http://unleash-test-7.app';
nock(url).persist().get('/client/features').reply(200, '{"Invalid payload');

Expand Down Expand Up @@ -537,7 +537,7 @@ test('should respect timeout', t =>
*/

test('should emit errors on invalid features', (t) =>
new Promise((resolve) => {
new Promise<void>((resolve) => {
const url = 'http://unleash-test-1.app';
setup(url, [
{
Expand Down Expand Up @@ -567,7 +567,7 @@ test('should emit errors on invalid features', (t) =>
}));

test('should emit errors on invalid variant', (t) =>
new Promise((resolve) => {
new Promise<void>((resolve) => {
const url = 'http://unleash-test-1-invalid-bariant.app';
setup(url, [
{
Expand Down Expand Up @@ -603,7 +603,7 @@ test('should emit errors on invalid variant', (t) =>
}));

test('should load bootstrap first if faster than unleash-api', (t) =>
new Promise((resolve) => {
new Promise<void>((resolve) => {
const url = 'http://unleash-test-2-api-url.app';
const bootstrap = 'http://unleash-test-2-boostrap-url.app';
nock(url)
Expand Down Expand Up @@ -673,7 +673,7 @@ test('should load bootstrap first if faster than unleash-api', (t) =>
}));

test('bootstrap should not override actual data', (t) =>
new Promise((resolve) => {
new Promise<void>((resolve) => {
const url = 'http://unleash-test-2-api-url.app';
const bootstrap = 'http://unleash-test-2-boostrap-url.app';
nock(url)
Expand Down Expand Up @@ -743,7 +743,7 @@ test('bootstrap should not override actual data', (t) =>
}));

test('should load bootstrap first from file', (t) =>
new Promise((resolve) => {
new Promise<void>((resolve) => {
const url = 'http://unleash-test-3-api-url.app';
nock(url).persist().get('/client/features').delay(100).reply(408);

Expand Down Expand Up @@ -791,7 +791,7 @@ test('should load bootstrap first from file', (t) =>
}));

test('should not crash on bogus bootstrap', (t) =>
new Promise((resolve) => {
new Promise<void>((resolve) => {
const url = 'http://unleash-test-4-api-url.app';
nock(url).persist().get('/client/features').delay(100).reply(408);

Expand Down Expand Up @@ -821,7 +821,7 @@ test('should not crash on bogus bootstrap', (t) =>
}));

test('should load backup-file', (t) =>
new Promise((resolve) => {
new Promise<void>((resolve) => {
const appNameLocal = 'some-backup';
const url = 'http://unleash-test-backup-api-url.app';
nock(url).persist().get('/client/features').delay(100).reply(408);
Expand Down Expand Up @@ -867,7 +867,7 @@ test('should load backup-file', (t) =>
}));

test('bootstrap should override load backup-file', (t) =>
new Promise((resolve) => {
new Promise<void>((resolve) => {
const appNameLocal = 'should_override';
const url = 'http://unleash-test-backup-api-url.app';
nock(url).persist().get('/client/features').delay(100).reply(408);
Expand Down
2 changes: 1 addition & 1 deletion src/test/repository/storage-provider.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import test from 'ava';
import test from '../test-shim';
import { writeFileSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
Expand Down
2 changes: 1 addition & 1 deletion src/test/request.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import test from 'ava';
import test from './test-shim';

import * as http from 'http';
import * as https from 'https';
Expand Down
31 changes: 31 additions & 0 deletions src/test/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Shared test setup for Ava.
// Suppress noisy background fetches that hit Nock after tests complete.
process.setMaxListeners(50);

const shouldIgnore = (err: any) => {
if (!err) return false;
return (
err.code === 'ERR_NOCK_NO_MATCH' ||
err.code === 'ENOTFOUND' ||
(typeof err.message === 'string' && err.message.includes('Nock: No match')) ||
(typeof err.message === 'string' &&
(err.message.includes('EAI_AGAIN') || err.message.includes('ENOTFOUND')))
);
};

process.on('unhandledRejection', (reason: any, promise) => {
if (!shouldIgnore(reason)) {
// eslint-disable-next-line no-console
console.error(reason);
}
if (promise && typeof promise.catch === 'function') {
promise.catch(() => {});
}
});

process.on('uncaughtException', (err: any) => {
if (!shouldIgnore(err)) {
// eslint-disable-next-line no-console
console.error(err);
}
});
2 changes: 1 addition & 1 deletion src/test/strategy/application-hostname-strategy.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import test from 'ava';
import test from '../test-shim';
import { hostname } from 'os';

import ApplicationHostnameStrategy from '../../strategy/application-hostname-strategy';
Expand Down
2 changes: 1 addition & 1 deletion src/test/strategy/default-strategy.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import test from 'ava';
import test from '../test-shim';

import DefaultStrategy from '../../strategy/default-strategy';

Expand Down
2 changes: 1 addition & 1 deletion src/test/strategy/flexible-rollout.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable import/no-extraneous-dependencies */
import test from 'ava';
import test from '../test-shim';
import * as sinon from 'sinon';

import FlexibleRolloutStrategy from '../../strategy/flexible-rollout-strategy';
Expand Down
2 changes: 1 addition & 1 deletion src/test/strategy/gradual-rollout-random.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import test from 'ava';
import test from '../test-shim';

import GradualRolloutRandomStrategy from '../../strategy/gradual-rollout-random';

Expand Down
2 changes: 1 addition & 1 deletion src/test/strategy/gradual-rollout-session-id.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import test from 'ava';
import test from '../test-shim';

import GradualRolloutSessionIdStrategy from '../../strategy/gradual-rollout-session-id';
import { normalizedStrategyValue } from '../../strategy/util';
Expand Down
2 changes: 1 addition & 1 deletion src/test/strategy/gradual-rollout-user-id.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import test from 'ava';
import test from '../test-shim';

import GradualRolloutUserIdStrategy from '../../strategy/gradual-rollout-user-id';
import { normalizedStrategyValue } from '../../strategy/util';
Expand Down
2 changes: 1 addition & 1 deletion src/test/strategy/remote-address-strategy.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import test from 'ava';
import test from '../test-shim';

import RemoteAddressStrategy from '../../strategy/remote-addresss-strategy';

Expand Down
2 changes: 1 addition & 1 deletion src/test/strategy/strategy.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable import/no-extraneous-dependencies */
import test from 'ava';
import test from '../test-shim';

import { Strategy } from '../../strategy/strategy';

Expand Down
2 changes: 1 addition & 1 deletion src/test/strategy/user-with-id-strategy.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import test from 'ava';
import test from '../test-shim';

import UserWithIdStrategy from '../../strategy/user-with-id-strategy';

Expand Down
Loading