Skip to content

Commit

Permalink
oh god rollup/typescript/jest/eslint/prettier/lerna
Browse files Browse the repository at this point in the history
  • Loading branch information
brainkim committed Apr 7, 2019
1 parent 7a9a066 commit dd46ab4
Show file tree
Hide file tree
Showing 21 changed files with 2,421 additions and 197 deletions.
7 changes: 1 addition & 6 deletions .gitignore
@@ -1,11 +1,6 @@
node_modules

lib
.next
.nyc_output

.rpt2_cache
npm-debug.log*
yarn-debug.log*
yarn-error.log*

.env
59 changes: 36 additions & 23 deletions jest.config.js
Expand Up @@ -5,14 +5,14 @@ module.exports = {
// All imported modules in your tests should be mocked automatically
// automock: false,

// Stop running tests after the first failure
// bail: false,
// Stop running tests after `n` failures
// bail: 0,

// Respect "browser" field in package.json when resolving modules
// browser: false,

// The directory where Jest should store its cached dependency information
// cacheDirectory: "/var/folders/5t/bqn5fb4s0bs4qdbjv9zdn28r0000gn/T/jest_dx",
// cacheDirectory: "/private/var/folders/5t/bqn5fb4s0bs4qdbjv9zdn28r0000gn/T/jest_dx",

// Automatically clear mock calls and instances between every test
clearMocks: true,
Expand All @@ -24,7 +24,7 @@ module.exports = {
// collectCoverageFrom: null,

// The directory where Jest should output its coverage files
// coverageDirectory: null,
// coverageDirectory: "coverage",

// An array of regexp pattern strings used to skip coverage collection
// coveragePathIgnorePatterns: [
Expand All @@ -42,10 +42,13 @@ module.exports = {
// An object that configures minimum threshold enforcement for coverage results
// coverageThreshold: null,

// A path to a custom dependency extractor
// dependencyExtractor: null,

// Make calling deprecated APIs throw helpful error messages
// errorOnDeprecated: false,

// Force coverage collection from ignored files usin a array of glob patterns
// Force coverage collection from ignored files using an array of glob patterns
// forceCoverageMatch: [],

// A path to a module which exports an async function that is triggered once before all test suites
Expand All @@ -56,7 +59,14 @@ module.exports = {

// A set of global variables that need to be available in all test environments
globals: {
"ts-jest": {}
"ts-jest": {
tsConfig: {
target: "esnext",
},
diagnostics: {
ignoreCodes: [151001],
},
},
},

// An array of directory names to be searched recursively up from the requiring module's location
Expand All @@ -66,9 +76,12 @@ module.exports = {

// An array of file extensions your modules use
moduleFileExtensions: [
"js",
"json",
"jsx",
"ts",
//"tsx",
"js"
"tsx",
"node"
],

// A map from regular expressions to module names that allow to stub out resources with a single module
Expand All @@ -81,7 +94,7 @@ module.exports = {
// notify: false,

// An enum that specifies notification mode. Requires { notify: true }
// notifyMode: "always",
// notifyMode: "failure-change",

// A preset that is used as a base for Jest's configuration
// preset: null,
Expand Down Expand Up @@ -118,14 +131,14 @@ module.exports = {
// The paths to modules that run some code to configure or set up the testing environment before each test
// setupFiles: [],

// The path to a module that runs some code to configure or set up the testing framework before each test
// setupTestFrameworkScriptFile: null,
// A list of paths to modules that run some code to configure or set up the testing framework before each test
// setupFilesAfterEnv: [],

// A list of paths to snapshot serializer modules Jest should use for snapshot testing
// snapshotSerializers: [],

// The test environment that will be used for testing
testEnvironment: "node",
// testEnvironment: "jest-environment-jsdom",

// Options that will be passed to the testEnvironment
// testEnvironmentOptions: {},
Expand All @@ -134,18 +147,18 @@ module.exports = {
// testLocationInResults: false,

// The glob patterns Jest uses to detect test files
testMatch: [
"**/__tests__/*.+(ts|tsx|js)"
],
// testMatch: [
// "**/__tests__/**/*.[jt]s?(x)",
// "**/?(*.)+(spec|test).[tj]s?(x)"
// ],

// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
testPathIgnorePatterns: [
"/node_modules/",
"/lib/"
],
// testPathIgnorePatterns: [
// "/node_modules/"
// ],

// The regexp pattern Jest uses to detect test files
// testRegex: "",
// The regexp pattern or array of patterns that Jest uses to detect test files
// testRegex: [],

// This option allows the use of a custom results processor
// testResultsProcessor: null,
Expand All @@ -161,7 +174,7 @@ module.exports = {

// A map from regular expressions to paths to transformers
transform: {
"^.+\\.tsx?$": "ts-jest"
"^.+\\.tsx?$": "ts-jest",
},

// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
Expand All @@ -173,7 +186,7 @@ module.exports = {
// unmockedModulePathPatterns: undefined,

// Indicates whether each individual test should be reported during the run
// verbose: null,
verbose: true,

// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
// watchPathIgnorePatterns: [],
Expand Down
20 changes: 18 additions & 2 deletions package.json
@@ -1,10 +1,26 @@
{
"private": true,
"scripts": {
"test": "jest"
"build": "lerna run build --parallel",
"test": "lerna run test --stream",
"lint": "eslint packages --ext .ts --ignore-pattern lib"
},
"devDependencies": {
"lerna": "^3.0.6"
"@types/jest": "^24.0.11",
"@typescript-eslint/eslint-plugin": "^1.5.0",
"@typescript-eslint/parser": "^1.5.0",
"eslint": "^5.15.3",
"eslint-config-prettier": "^4.1.0",
"eslint-plugin-prettier": "^3.0.1",
"jest": "^24.5.0",
"lerna": "^3.0.6",
"prettier": "^1.16.4",
"rollup": "^1.9.0",
"rollup-plugin-typescript2": "^0.20.1",
"shx": "^0.3.2",
"ts-jest": "^24.0.0",
"typescript": "^3.3.4000",
"weak": "^1.0.1"
},
"workspaces": [
"packages/*"
Expand Down
107 changes: 78 additions & 29 deletions packages/channel/index.test.ts → packages/channel/__tests__/index.ts
Expand Up @@ -4,7 +4,7 @@ import {
DroppingBuffer,
FixedBuffer,
SlidingBuffer,
} from "./index";
} from "../index";

describe("buffers", () => {
describe("FixedBuffer", () => {
Expand Down Expand Up @@ -35,29 +35,33 @@ describe("buffers", () => {
describe("SlidingBuffer", () => {
test("simple", () => {
const buffer = new SlidingBuffer<number>(2);
expect([buffer.empty, buffer.full]).toEqual([true, false]);
buffer.add(1);
buffer.add(2);
buffer.add(3);
buffer.add(4);
buffer.add(5);
expect(buffer.full).toBe(false);
expect([buffer.empty, buffer.full]).toEqual([false, false]);
expect(buffer.remove()).toEqual(4);
expect(buffer.remove()).toEqual(5);
expect([buffer.empty, buffer.full]).toEqual([true, false]);
expect(buffer.remove()).toEqual(undefined);
});
});

describe("DroppingBuffer", () => {
test("simple", () => {
const buffer = new DroppingBuffer<number>(2);
expect([buffer.empty, buffer.full]).toEqual([true, false]);
buffer.add(1);
buffer.add(2);
buffer.add(3);
buffer.add(4);
buffer.add(5);
expect(buffer.full).toBe(false);
expect([buffer.empty, buffer.full]).toEqual([false, false]);
expect(buffer.remove()).toEqual(1);
expect(buffer.remove()).toEqual(2);
expect([buffer.empty, buffer.full]).toEqual([true, false]);
expect(buffer.remove()).toEqual(undefined);
});
});
Expand Down Expand Up @@ -93,31 +97,65 @@ describe("Channel", () => {
const chan = new Channel<number>(async () => {
throw error;
});
await expect(chan.next()).rejects.toBe(error);
await expect(chan.next()).rejects.toBeDefined();
});

test("sync error in executor after close causes return to reject", async () => {
const error = new Error("Sync error after close");
const chan = new Channel<number>((push, close) => {
push(1);
close();
throw error;
});
await expect(chan.next()).resolves.toEqual({ value: 1, done: false });
await expect(chan.next()).resolves.toEqual({ done: true });
await expect(chan.next()).resolves.toEqual({ done: true });
await expect(chan.return()).rejects.toBe(error);
});

test("async error in executor after close causes return to reject", async () => {
const error = new Error("Async error after close");
const chan = new Channel<number>(async (push, close) => {
await push(1);
close();
throw error;
});
await expect(chan.next()).resolves.toEqual({ value: 1, done: false });
await expect(chan.next()).resolves.toEqual({ done: true });
await expect(chan.next()).resolves.toEqual({ done: true });
await expect(chan.return()).rejects.toBe(error);
});

test("pull then push avoids buffer", async () => {
const buffer = new FixedBuffer<number>(1);
let push: (value: number) => Promise<void>;
const chan = new Channel((push1) => (push = push1), buffer);
const pullResult = chan.next();
push!(1000);
const chan = new Channel((push1) => {
push = push1;
push(1);
}, buffer);
// prime the channel
await chan.next();
const result = chan.next();
push!(2);
expect(buffer.empty).toBe(true);
await expect(pullResult).resolves.toEqual({ value: 1000, done: false });
await expect(result).resolves.toEqual({ value: 2, done: false });
});

test("pushes throw when buffer and push queue are full", () => {
test("pushes throw when buffer and push queue are full", async () => {
const bufferLength = 3;
let push: (value: number) => Promise<void>;
const chan = new Channel<number>(
(push1) => (push = push1),
new FixedBuffer(bufferLength),
);
for (let i = 0; i < bufferLength + chan["MAX_QUEUE_LENGTH"]; i++) {
const chan = new Channel<number>((push1) => {
push = push1;
push(-1);
}, new FixedBuffer(bufferLength));
// prime the channel
await chan.next();
let i = 0;
for (; i < bufferLength + chan["MAX_QUEUE_LENGTH"]; i++) {
push!(i);
}
expect(() => push(-1)).toThrow(ChannelOverflowError);
expect(() => push(-2)).toThrow(ChannelOverflowError);
expect(() => push(i++)).toThrow(ChannelOverflowError);
expect(() => push(i++)).toThrow(ChannelOverflowError);
});

test("pulls throw when pull queue is full", async () => {
Expand All @@ -131,35 +169,31 @@ describe("Channel", () => {

test("dropping buffer", async () => {
const chan = new Channel<number>((push, close) => {
push(1);
push(2);
push(3);
push(4);
push(5);
for (let i = 0; i < 100; i++) {
push(i);
}
close();
}, new DroppingBuffer(3));
const result: number[] = [];
for await (const num of chan) {
result.push(num);
}
expect(result).toEqual([1, 2, 3]);
expect(result).toEqual([0, 1, 2, 3]);
await expect(chan.next()).resolves.toEqual({ done: true });
});

test("sliding buffer", async () => {
const chan = new Channel<number>((push, close) => {
push(1);
push(2);
push(3);
push(4);
push(5);
for (let i = 0; i < 100; i++) {
push(i);
}
close();
}, new SlidingBuffer(3));
const result: number[] = [];
for await (const num of chan) {
result.push(num);
}
expect(result).toEqual([3, 4, 5]);
expect(result).toEqual([0, 97, 98, 99]);
await expect(chan.next()).resolves.toEqual({ done: true });
});

Expand Down Expand Up @@ -211,7 +245,7 @@ describe("Channel", () => {
await push(3);
await push(4);
});
let result: number[] = [];
const result: number[] = [];
for await (const num of chan) {
result.push(num);
if (num === 3) {
Expand All @@ -221,4 +255,19 @@ describe("Channel", () => {
expect(result).toEqual([1, 2, 3]);
await expect(chan.next()).resolves.toEqual({ done: true });
});

test("stop", async () => {
const mock = jest.fn();
const chan = new Channel<number>(async (push, _, stop) => {
push(1);
push(2);
await Promise.race([stop, push(3)]);
mock();
});
await expect(chan.next()).resolves.toEqual({ done: false, value: 1 });
await expect(chan.next()).resolves.toEqual({ done: false, value: 2 });
chan.return();
await expect(chan.next()).resolves.toEqual({ done: true });
expect(mock).toBeCalled();
});
});

0 comments on commit dd46ab4

Please sign in to comment.