Skip to content

Commit

Permalink
Test with web-test-runner too
Browse files Browse the repository at this point in the history
  • Loading branch information
ranile committed May 26, 2023
1 parent 418ae26 commit bc2b434
Show file tree
Hide file tree
Showing 9 changed files with 5,611 additions and 2,320 deletions.
7,780 changes: 5,507 additions & 2,273 deletions package-lock.json

Large diffs are not rendered by default.

19 changes: 15 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"description": "Diff Updater",
"main": "node/index.js",
"browser": "browser/index.mjs",
"prviate": true,
"types": "types.d.ts",
"repository": "https://github.com/formbird/json-patcher",
"exclude": [
Expand All @@ -15,18 +14,30 @@
"node:build": "npx cargo-cp-artifact -nc node/diff_updater.node -- cargo build --message-format=json-render-diagnostics",
"node:build:release": "$npm_execpath run node:build -- --release",
"build:vite": "vite build",
"browser:build": "cargo build --target wasm32-unknown-unknown && wasm-bindgen --out-dir browser/ --target bundler target/wasm32-unknown-unknown/debug/diff_updater.wasm && $npm_execpath run build:vite",
"browser:build:release": "cargo build --target wasm32-unknown-unknown --release && wasm-bindgen --out-dir artifacts/ --target bundler target/wasm32-unknown-unknown/release/diff_updater.wasm && $npm_execpath run build:vite",
"browser:build": "cargo build --target wasm32-unknown-unknown && wasm-bindgen --out-dir browser/ --target bundler target/wasm32-unknown-unknown/debug/json_patcher.wasm && $npm_execpath run build:vite",
"browser:build:release": "cargo build --target wasm32-unknown-unknown --release && wasm-bindgen --out-dir browser/ --target bundler target/wasm32-unknown-unknown/release/json_patcher.wasm && $npm_execpath run build:vite",
"build": "$npm_execpath run node:build && $npm_execpath run browser:build",
"build:release": "$npm_execpath run node:build:release && $npm_execpath run browser:build:release",
"install": "$npm_execpath run node:build:release",
"test": "jest"
"test": "jest",
"wtr": "wtr test/**/*.spec.ts"
},
"devDependencies": {
"@esm-bundle/chai": "^4.3.4-fix.0",
"@rollup/plugin-commonjs": "^25.0.0",
"@rollup/plugin-node-resolve": "^15.0.2",
"@types/chai": "^4.3.5",
"@types/jest": "^29.5.1",
"@types/node": "^20.1.5",
"@web/dev-server-esbuild": "^0.4.1",
"@web/dev-server-import-maps": "^0.1.1",
"@web/test-runner": "^0.16.1",
"@web/test-runner-playwright": "^0.10.0",
"cargo-cp-artifact": "^0.1",
"chai": "^4.3.7",
"expect": "^29.5.0",
"jest": "^29.5.0",
"rollup-plugin-polyfill-node": "^0.12.0",
"ts-jest": "^29.1.0",
"typescript": "^5.0.4",
"vite": "^4.2.2",
Expand Down
5 changes: 3 additions & 2 deletions test/arrays.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { expect } from 'chai'
import {applyPatch} from '..'
import {createParsedPatch as createPatch} from "./_index";

Expand Down Expand Up @@ -39,9 +40,9 @@ const pairs = [


pairs.forEach(([input, output]) => {
test(`diff+patch: [${input}] => [${output}]`, () => {
it(`diff+patch: [${input}] => [${output}]`, () => {
const patch = createPatch(input, output)
const actualOutput = applyPatch(input, patch)
expect(actualOutput).toEqual(output)
expect(actualOutput).to.deep.equal(output);
})
})
33 changes: 17 additions & 16 deletions test/patch.spec.ts
Original file line number Diff line number Diff line change
@@ -1,74 +1,75 @@
import { expect } from 'chai'
import {applyPatch} from '..'

test('broken add', () => {
it('broken add', () => {
const user = {id: 'chbrown'}
expect(() => {
applyPatch(user, [
{op: 'add', path: '/a/b', value: 1},
])
}).toThrow()
}).to.throw()
})

test('broken remove', () => {
it('broken remove', () => {
const user = {id: 'chbrown'}
expect(() => {
applyPatch(user, [
{op: 'remove', path: '/name'},
])
}).toThrow()
}).to.throw()

})

test('broken replace', () => {
it('broken replace', () => {
const user = {id: 'chbrown'}
expect(() => {
applyPatch(user, [
{op: 'replace', path: '/name', value: 1},
])
}).toThrow()
}).to.throw()
})

test('broken replace (array)', () => {
it('broken replace (array)', () => {
const users = [{id: 'chbrown'}]
expect(() => {
applyPatch(users, [
{op: 'replace', path: '/1', value: {id: 'chbrown2'}},
])
}).toThrow()
}).to.throw()
})

test('broken move (from)', () => {
it('broken move (from)', () => {
const user = {id: 'chbrown'}
expect(() => {
applyPatch(user, [
{op: 'move', from: '/name', path: '/id'},
])
}).toThrow()
}).to.throw()
})

test('broken move (path)', () => {
it('broken move (path)', () => {
const user = {id: 'chbrown'}
expect(() => {
applyPatch(user, [
{op: 'move', from: '/id', path: '/a/b'},
])
}).toThrow()
}).to.throw()
})

test('broken copy (from)', () => {
it('broken copy (from)', () => {
const user = {id: 'chbrown'}
expect(() => {
applyPatch(user, [
{op: 'copy', from: '/name', path: '/id'},
])
}).toThrow()
}).to.throw()
})

test('broken copy (path)', () => {
it('broken copy (path)', () => {
const user = {id: 'chbrown'}
expect(() => {
applyPatch(user, [
{op: 'copy', from: '/id', path: '/a/b'},
])
}).toThrow()
}).to.throw()
})
40 changes: 23 additions & 17 deletions test/roundtrips.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import {applyPatch, type PatchOperation} from "..";
import { createParsedPatch as createPatch } from './_index'
import { expect } from 'chai'

function checkRoundtrip(
input: any,
output: any,
expected_patch: PatchOperation[],
actual_patch: PatchOperation[] = createPatch(input, output)
) {
expect(actual_patch).toEqual(expected_patch);
expect(actual_patch).to.deep.equal(expected_patch);

const actualOutput = applyPatch(input, actual_patch);
expect(actualOutput).toEqual(output);
if (actualOutput instanceof Map) {
expect(actualOutput).not.to.instanceOf(Map)
}
expect(actualOutput).to.deep.equal(output);
}

test("issues/3", () => {
it("issues/3", () => {
const input = {arr: ["1", "2", "2"]};
const output = {arr: ["1"]};
const expected_patch: PatchOperation[] = [
Expand All @@ -22,7 +27,7 @@ test("issues/3", () => {
checkRoundtrip(input, output, expected_patch);
});

test("issues/4", () => {
it("issues/4", () => {
const input = ["A", "B"];
const output = ["B", "A"];
const expected_patch: PatchOperation[] = [
Expand All @@ -33,7 +38,7 @@ test("issues/4", () => {
checkRoundtrip(input, output, expected_patch);
});

test("issues/5", () => {
it("issues/5", () => {
const input: string[] = [];
const output = ["A", "B"];
const expected_patch: PatchOperation[] = [
Expand All @@ -44,7 +49,7 @@ test("issues/5", () => {
checkRoundtrip(input, output, expected_patch);
});

test("issues/9", () => {
it("issues/9", () => {
const input = [{A: 1, B: 2}, {C: 3}];
const output = [{A: 1, B: 20}, {C: 3}];
const expected_patch: PatchOperation[] = [
Expand All @@ -53,8 +58,9 @@ test("issues/9", () => {

checkRoundtrip(input, output, expected_patch);
});


test("issues/12", () => {
it("issues/12", () => {
const input = {name: "ABC", repositories: ["a", "e"]};
const output = {name: "ABC", repositories: ["a", "b", "c", "d", "e"]};
const expected_patch: PatchOperation[] = [
Expand All @@ -66,7 +72,7 @@ test("issues/12", () => {
checkRoundtrip(input, output, expected_patch);
});

test("issues/15", () => {
it("issues/15", () => {
const input = {date: new Date(0)};
const output = {date: '1970-01-01T00:00:00.001+00:00'};
const expected_patch: PatchOperation[] = [
Expand All @@ -76,7 +82,7 @@ test("issues/15", () => {
checkRoundtrip(input, output, expected_patch);
});

test("issues/15/array", () => {
it("issues/15/array", () => {
const input = [new Date(0)];
const output = ['1970-01-01T00:00:00.001+00:00'];
const expected_patch: PatchOperation[] = [
Expand All @@ -86,15 +92,15 @@ test("issues/15/array", () => {
checkRoundtrip(input, output, expected_patch);
});

test("issues/32", () => {
it("issues/32", () => {
const input = "a";
const output = "b";
const expected_patch: PatchOperation[] = [ { op: 'replace', path: '', value: 'b' } ]

checkRoundtrip(input, output, expected_patch)
});

test("issues/35", () => {
it("issues/35", () => {
const input = {name: "bob", image: undefined, cat: null};
const output = {name: "bob", image: "foo.jpg", cat: "nikko"};
const expected_patch: PatchOperation[] = [
Expand All @@ -105,7 +111,7 @@ test("issues/35", () => {
checkRoundtrip(input, output, expected_patch);
});

test("issues/36", () => {
it("issues/36", () => {
const input = [undefined, "B"]; // same as: const input = ['A', 'B']; delete input[0]
const output = ["A", "B"];
const expected_patch: PatchOperation[] = [
Expand All @@ -116,7 +122,7 @@ test("issues/36", () => {
});

/*
test('issues/37', t => {
it('issues/37', t => {
const value = {id: 'chbrown'}
const patch_results = applyPatch(value, [
{op: 'copy', from: '/id', path: '/name'},
Expand All @@ -126,7 +132,7 @@ test('issues/37', t => {
t.true(patch_results.every(result => result == null), 'should apply patch successfully')
})
test('issues/38', t => {
it('issues/38', t => {
const value = {
current: {timestamp: 23},
history: [],
Expand All @@ -147,7 +153,7 @@ test('issues/38', t => {
t.true(patch_results.every(result => result == null), 'should apply patch successfully')
})
test('issues/44', t => {
it('issues/44', t => {
const value = {}
const author = {firstName: 'Chris'}
const patch_results = applyPatch(value, [
Expand All @@ -162,7 +168,7 @@ test('issues/44', t => {
t.deepEqual(author, {firstName: 'Chris'}, 'patch reference should not be changed')
})
test('issues/76', t => {
it('issues/76', t => {
t.true(({} as any).polluted === undefined, 'Object prototype should not be polluted')
const value = {}
applyPatch(value, [
Expand All @@ -171,7 +177,7 @@ test('issues/76', t => {
t.true(({} as any).polluted === undefined, 'Object prototype should still not be polluted')
})
test('issues/78', t => {
it('issues/78', t => {
const user = {firstName: 'Chris'}
const patch_results = applyPatch(user, [
{op: 'add', path: '/createdAt', value: new Date('2010-08-10T22:10:48Z')},
Expand Down
6 changes: 3 additions & 3 deletions test/specification.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function runCatching(spec: Spec, f: () => void) {
}
}

test('Specification format', () => {
it('Specification format', () => {
expect(spec_data.length).toEqual(19)
// use sorted values and sort() to emulate set equality
const props = ['diffable', 'input', 'name', 'output', 'patch', 'results']
Expand All @@ -38,7 +38,7 @@ test('Specification format', () => {
// take the input, apply the patch, and check the actual result against the
// expected output
spec_data.forEach(spec => {
test(`patch ${spec.name}`, () => {
it(`patch ${spec.name}`, () => {
// patch operations are applied to object in-place
const expected = spec.output
runCatching(spec, () => {
Expand All @@ -49,7 +49,7 @@ spec_data.forEach(spec => {
})

spec_data.filter(spec => spec.diffable).forEach(spec => {
test(`diff ${spec.name}`, () => {
it(`diff ${spec.name}`, () => {
if (spec.ignored) {
return
}
Expand Down
6 changes: 3 additions & 3 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "Node16",
"module": "ESNext",
"moduleResolution": "nodenext",
"esModuleInterop": true,
"target": "ES6"
"target": "ESNext"
}
}
5 changes: 3 additions & 2 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import wasm from "vite-plugin-wasm";
export default defineConfig({
build: {
lib: {
entry: './browser/diff_updater.js',
entry: './browser/json_patcher.js',
name: 'json-patcher',
fileName: 'index',
formats: ['es']
},
target: 'esnext',
outDir: 'browser',
emptyOutDir: false
},
plugins: [
wasm(),
topLevelAwait(),
// topLevelAwait(),
]
})
Loading

0 comments on commit bc2b434

Please sign in to comment.