Skip to content

Commit

Permalink
chore: Merge branch 'master' of github.com:immerjs/immer
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Nov 17, 2020
2 parents 0730231 + 31684f2 commit 6c62eec
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 128 deletions.
9 changes: 5 additions & 4 deletions .travis.yml
@@ -1,7 +1,7 @@
language: node_js
node_js:
- "10.18.1"
# - "node"
# - "10.18.1"
- "node"
env:
- NODE_ENV=TEST
cache:
Expand All @@ -12,12 +12,13 @@ before_script:
- yarn global add if-node-version
script:
- yarn build || travis_terminate 1
- if-node-version 10 || { yarn test && travis_terminate 0; }
- yarn coveralls
- yarn test || travis_terminate 1
- yarn coveralls || travis_terminate 0
jobs:
include:
- stage: deploy
if: branch == master && !fork
script:
- yarn test || travis_terminate 1
- NODE_ENV= yarn build || travis_terminate 1
- yarn semantic-release
80 changes: 32 additions & 48 deletions __tests__/__prod_snapshots__/base.js.snap

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions __tests__/__prod_snapshots__/patch.js.snap
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`applyPatches throws when \`op\` is not "add", "replace", nor "remove" 1`] = `"[Immer] minified error nr: 17 copy. Find the full error at: https://bit.ly/3cXEKWf"`;
exports[`applyPatches throws when \`op\` is not "add", "replace", nor "remove" 1`] = `"[Immer] minified error nr: 17 'copy'. Find the full error at: https://bit.ly/3cXEKWf"`;

exports[`applyPatches throws when \`path\` cannot be resolved 1`] = `"[Immer] minified error nr: 15 a/b. Find the full error at: https://bit.ly/3cXEKWf"`;
exports[`applyPatches throws when \`path\` cannot be resolved 1`] = `"[Immer] minified error nr: 15 'a/b'. Find the full error at: https://bit.ly/3cXEKWf"`;

exports[`applyPatches throws when \`path\` cannot be resolved 2`] = `"[Immer] minified error nr: 15 a/b/c. Find the full error at: https://bit.ly/3cXEKWf"`;
exports[`applyPatches throws when \`path\` cannot be resolved 2`] = `"[Immer] minified error nr: 15 'a/b/c'. Find the full error at: https://bit.ly/3cXEKWf"`;
10 changes: 5 additions & 5 deletions __tests__/__prod_snapshots__/plugins.js.snap
@@ -1,11 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ES5 plugins should throw if no proxies are available error when using ES5 1`] = `"[Immer] minified error nr: 19 ES5. Find the full error at: https://bit.ly/3cXEKWf"`;
exports[`ES5 plugins should throw if no proxies are available error when using ES5 1`] = `"[Immer] minified error nr: 18 'ES5'. Find the full error at: https://bit.ly/3cXEKWf"`;

exports[`error when using Maps 1`] = `"[Immer] minified error nr: 19 MapSet. Find the full error at: https://bit.ly/3cXEKWf"`;
exports[`error when using Maps 1`] = `"[Immer] minified error nr: 18 'MapSet'. Find the full error at: https://bit.ly/3cXEKWf"`;

exports[`error when using patches - 1 1`] = `"[Immer] minified error nr: 19 Patches. Find the full error at: https://bit.ly/3cXEKWf"`;
exports[`error when using patches - 1 1`] = `"[Immer] minified error nr: 18 'Patches'. Find the full error at: https://bit.ly/3cXEKWf"`;

exports[`error when using patches - 2 1`] = `"[Immer] minified error nr: 19 Patches. Find the full error at: https://bit.ly/3cXEKWf"`;
exports[`error when using patches - 2 1`] = `"[Immer] minified error nr: 18 'Patches'. Find the full error at: https://bit.ly/3cXEKWf"`;

exports[`error when using patches - 3 1`] = `"[Immer] minified error nr: 19 Patches. Find the full error at: https://bit.ly/3cXEKWf"`;
exports[`error when using patches - 3 1`] = `"[Immer] minified error nr: 18 'Patches'. Find the full error at: https://bit.ly/3cXEKWf"`;
107 changes: 56 additions & 51 deletions __tests__/base.js
Expand Up @@ -16,6 +16,8 @@ jest.setTimeout(1000)

enableAllPlugins()

const isProd = process.env.NODE_ENV === "production"

test("immer should have no dependencies", () => {
expect(require("../package.json").dependencies).toBeUndefined()
})
Expand Down Expand Up @@ -1145,65 +1147,66 @@ function runBaseTest(name, useProxies, autoFreeze, useListener) {
})

// NOTE: ES5 drafts only protect existing properties when revoked.
it("revokes the draft once produce returns", () => {
const expectRevoked = (fn, shouldThrow = true) => {
if (shouldThrow) expect(fn).toThrowErrorMatchingSnapshot()
else expect(fn).not.toThrow()
}
if (!isProd)
it("revokes the draft once produce returns", () => {
const expectRevoked = (fn, shouldThrow = true) => {
if (shouldThrow) expect(fn).toThrowErrorMatchingSnapshot()
else expect(fn).not.toThrow()
}

// Test object drafts:
let draft
produce({a: 1, b: 1}, s => {
draft = s
delete s.b
})
// Test object drafts:
let draft
produce({a: 1, b: 1}, s => {
draft = s
delete s.b
})

// Access known property on object draft.
expectRevoked(() => {
draft.a
})
// Access known property on object draft.
expectRevoked(() => {
draft.a
})

// Assign known property on object draft.
expectRevoked(() => {
draft.a = true
})
// Assign known property on object draft.
expectRevoked(() => {
draft.a = true
})

// Access unknown property on object draft.
expectRevoked(() => {
draft.z
}, useProxies)
// Access unknown property on object draft.
expectRevoked(() => {
draft.z
}, useProxies)

// Assign unknown property on object draft.
expectRevoked(() => {
draft.z = true
}, useProxies)
// Assign unknown property on object draft.
expectRevoked(() => {
draft.z = true
}, useProxies)

// Test array drafts:
produce([1, 2], s => {
draft = s
s.pop()
})
// Test array drafts:
produce([1, 2], s => {
draft = s
s.pop()
})

// Access known index of an array draft.
expectRevoked(() => {
draft[0]
})
// Access known index of an array draft.
expectRevoked(() => {
draft[0]
})

// Assign known index of an array draft.
expectRevoked(() => {
draft[0] = true
})
// Assign known index of an array draft.
expectRevoked(() => {
draft[0] = true
})

// Access unknown index of an array draft.
expectRevoked(() => {
draft[1]
}, useProxies)
// Access unknown index of an array draft.
expectRevoked(() => {
draft[1]
}, useProxies)

// Assign unknown index of an array draft.
expectRevoked(() => {
draft[1] = true
}, useProxies)
})
// Assign unknown index of an array draft.
expectRevoked(() => {
draft[1] = true
}, useProxies)
})

it("can access a child draft that was created before the draft was modified", () => {
produce({a: {}}, s => {
Expand Down Expand Up @@ -1697,7 +1700,7 @@ function runBaseTest(name, useProxies, autoFreeze, useListener) {
},
e => {
expect(e).toBe(err)
expect(() => draft.a).toThrowErrorMatchingSnapshot()
if (!isProd) expect(() => draft.a).toThrowErrorMatchingSnapshot()
}
)
})
Expand Down Expand Up @@ -2418,7 +2421,9 @@ function testLiteralTypes(produce) {
draft.foo = true
})
).toThrowError(
"produce can only be called on things that are draftable"
isProd
? "[Immer] minified error nr: 21"
: "produce can only be called on things that are draftable"
)
})
} else {
Expand Down
8 changes: 7 additions & 1 deletion __tests__/current.js
Expand Up @@ -14,6 +14,8 @@ enableAllPlugins()
runTests("proxy", true)
runTests("es5", false)

const isProd = process.env.NODE_ENV === "production"

function runTests(name, useProxies) {
describe("current - " + name, () => {
beforeAll(() => {
Expand All @@ -24,7 +26,11 @@ function runTests(name, useProxies) {
it("must be called on draft", () => {
expect(() => {
current({})
}).toThrowError("[Immer] 'current' expects a draft, got: [object Object]")
}).toThrowError(
isProd
? "[Immer] minified error nr: 22 '[object Object]'. Find the full error at: https://bit.ly/3cXEKWf"
: "[Immer] 'current' expects a draft, got: [object Object]"
)
})

it("can handle simple arrays", () => {
Expand Down
21 changes: 12 additions & 9 deletions __tests__/manual.js
Expand Up @@ -10,6 +10,8 @@ import {

enableAllPlugins()

const isProd = process.env.NODE_ENV === "production"

runTests("proxy", true)
runTests("es5", false)

Expand Down Expand Up @@ -41,16 +43,17 @@ function runTests(name, useProxies) {
expect(state).toEqual([{}, {}, {}])
})

it("cannot modify after finish", () => {
const state = {a: 1}
if (!isProd)
it("cannot modify after finish", () => {
const state = {a: 1}

const draft = createDraft(state)
draft.a = 2
expect(finishDraft(draft)).toEqual({a: 2})
expect(() => {
draft.a = 3
}).toThrowErrorMatchingSnapshot()
})
const draft = createDraft(state)
draft.a = 2
expect(finishDraft(draft)).toEqual({a: 2})
expect(() => {
draft.a = 3
}).toThrowErrorMatchingSnapshot()
})

it("should support patches drafts", () => {
const state = {a: 1}
Expand Down
18 changes: 14 additions & 4 deletions __tests__/original.js
Expand Up @@ -3,6 +3,8 @@ import produce, {original, setUseProxies, enableAllPlugins} from "../src/immer"

enableAllPlugins()

const isProd = process.env.NODE_ENV === "production"

describe("original", () => {
const baseState = {
a: [],
Expand Down Expand Up @@ -40,20 +42,28 @@ describe("original", () => {
draftState.c = {}
draftState.d = 3
expect(() => original(draftState.c)).toThrowErrorMatchingInlineSnapshot(
`"[Immer] 'original' expects a draft, got: [object Object]"`
isProd
? `"[Immer] minified error nr: 23 '[object Object]'. Find the full error at: https://bit.ly/3cXEKWf"`
: `"[Immer] 'original' expects a draft, got: [object Object]"`
)
expect(() => original(draftState.d)).toThrowErrorMatchingInlineSnapshot(
`"[Immer] 'original' expects a draft, got: 3"`
isProd
? `"[Immer] minified error nr: 23 '3'. Find the full error at: https://bit.ly/3cXEKWf"`
: `"[Immer] 'original' expects a draft, got: 3"`
)
})
})

it("should return undefined for an object that is not proxied", () => {
expect(() => original({})).toThrowErrorMatchingInlineSnapshot(
`"[Immer] 'original' expects a draft, got: [object Object]"`
isProd
? `"[Immer] minified error nr: 23 '[object Object]'. Find the full error at: https://bit.ly/3cXEKWf"`
: `"[Immer] 'original' expects a draft, got: [object Object]"`
)
expect(() => original(3)).toThrowErrorMatchingInlineSnapshot(
`"[Immer] 'original' expects a draft, got: 3"`
isProd
? `"[Immer] minified error nr: 23 '3'. Find the full error at: https://bit.ly/3cXEKWf"`
: `"[Immer] 'original' expects a draft, got: 3"`
)
})
})
6 changes: 6 additions & 0 deletions ignoreObseleteSnapshots.js
@@ -0,0 +1,6 @@
module.exports = function(results) {
// don't count obselete snapshot as a failure, but just check if there are no failing tests
// console.dir(results)
results.success = results.testResults.every(r => r.numFailingTests === 0)
return results
}
3 changes: 2 additions & 1 deletion jest.config.build.js
Expand Up @@ -14,5 +14,6 @@ module.exports = {
preset: "ts-jest/presets/js-with-ts",
testEnvironment: "node",
testMatch: ["**/__tests__/**/*.[jt]s?(x)"],
snapshotResolver: "<rootDir>/jest.config.build.snapshots.js"
snapshotResolver: "<rootDir>/jest.config.build.snapshots.js",
testResultsProcessor: "<rootDir>/ignoreObseleteSnapshots.js"
}
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -10,7 +10,7 @@ Winner of the "Breakthrough of the year" [React open source award](https://osawa

## Contribute using one-click online setup

You can use Gitpod(a free online VS Code like IDE) for contributing online. With a single click it will launch a workspace and automatically:
You can use Gitpod (a free online VS Code like IDE) for contributing online. With a single click it will launch a workspace and automatically:

- clone the immer repo.
- install the dependencies.
Expand Down
2 changes: 1 addition & 1 deletion src/utils/errors.ts
Expand Up @@ -53,7 +53,7 @@ export function die(error: keyof typeof errors, ...args: any[]): never {
}
throw new Error(
`[Immer] minified error nr: ${error}${
args.length ? " " + args.join(",") : ""
args.length ? " " + args.map(s => `'${s}'`).join(",") : ""
}. Find the full error at: https://bit.ly/3cXEKWf`
)
}

0 comments on commit 6c62eec

Please sign in to comment.