Skip to content

Commit

Permalink
Replace Ava with Jest and Web Test Runner (#7)
Browse files Browse the repository at this point in the history
- Jest is run for Node tests
- Web Test Runner is used for browser tests
  • Loading branch information
ranile authored May 30, 2023
1 parent d615666 commit 11e256a
Show file tree
Hide file tree
Showing 18 changed files with 12,164 additions and 3,488 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,3 @@ neon-serde = { git = "https://github.com/formbird/neon-serde", package = "neon-s
[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = "0.2.86"
serde-wasm-bindgen = "0.5.0"
gloo-utils = "0.1"
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
14,083 changes: 11,238 additions & 2,845 deletions package-lock.json

Large diffs are not rendered by default.

25 changes: 18 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"version": "0.3.0",
"description": "Diff Updater",
"main": "node/index.js",
"browser": "artifacts/browser.mjs",
"prviate": true,
"browser": "browser/index.mjs",
"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",
"pretest": "tsc -p test/tsconfig.json",
"test": "ava"
"node:test": "jest",
"browser:test": "wtr test/**/*.spec.ts",
"test": "$npm_execpath run node:test && $npm_execpath run browser:test"
},
"devDependencies": {
"@esm-bundle/chai": "^4.3.4",
"@rollup/plugin-commonjs": "^25.0.0",
"@types/chai": "^4.3.5",
"@types/jest": "^29.5.1",
"@types/node": "^20.1.5",
"ava": "^5.2.0",
"@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",
"@web/dev-server-rollup": "^0.5.0",
"cargo-cp-artifact": "^0.1",
"chai": "^4.3.7",
"jest": "^29.5.0",
"ts-jest": "^29.1.0",
"typescript": "^5.0.4",
"vite": "^4.2.2",
"vite-plugin-top-level-await": "^1.3.0",
Expand Down
5 changes: 3 additions & 2 deletions src/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use wasm_bindgen::prelude::*;
use json_patch::PatchOperation;
use gloo_utils::format::JsValueSerdeExt;
use serde::ser::Serialize;

#[wasm_bindgen(js_name = createPatch)]
pub fn create_patch(left: JsValue, right: JsValue) -> Result<String, serde_wasm_bindgen::Error> {
Expand All @@ -15,5 +15,6 @@ pub fn apply_patch(doc: JsValue, patches: JsValue) -> Result<JsValue, serde_wasm
let mut doc = serde_wasm_bindgen::from_value(doc)?;
let patches: Vec<PatchOperation> = serde_wasm_bindgen::from_value(patches)?;
json_patch::patch(&mut doc, &patches).expect("todo");
JsValue::from_serde(&doc).map_err(|e| serde_wasm_bindgen::Error::new(&e.to_string()))
let ser = serde_wasm_bindgen::Serializer::json_compatible();
doc.serialize(&ser).map_err(|e| serde_wasm_bindgen::Error::new(&e.to_string()))
}
5 changes: 0 additions & 5 deletions test/_index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
This module is prefixed with an underscore so that ava recognizes it as a helper,
instead of failing the entire test suite with a "No tests found" error.
*/

import {createPatch} from '..'

export function createParsedPatch(input: any, output: any) {
Expand Down
7 changes: 3 additions & 4 deletions test/arrays.ts → test/arrays.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import test from 'ava'

import { expect } from 'chai'
import {applyPatch} from '..'
import {createParsedPatch as createPatch} from "./_index";

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


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

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

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

})

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

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

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

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

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

it('broken copy (path)', () => {
const user = {id: 'chbrown'}
expect(() => {
applyPatch(user, [
{op: 'copy', from: '/id', path: '/a/b'},
])
}).to.throw()
})
99 changes: 0 additions & 99 deletions test/patch.ts

This file was deleted.

Loading

0 comments on commit 11e256a

Please sign in to comment.