Skip to content

Commit

Permalink
chore: verify build during test
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Jan 15, 2020
1 parent 18cfe74 commit ef8990b
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 79 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ script:
- yarn build || travis_terminate 1
- if-node-version 10 || { yarn test && travis_terminate 0; }
- yarn coveralls
- yarn test:flow
jobs:
include:
- stage: deploy
Expand Down
60 changes: 34 additions & 26 deletions __tests__/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ function runBaseTest(name, useProxies, autoFreeze, useListener) {
const nextState = produce(baseState, s => {
// Map.prototype.set should return the Map itself
const res = s.aMap.set("force", true)
expect(res).toBe(s.aMap[DRAFT_STATE].draft)
if (!global.USES_BUILD) expect(res).toBe(s.aMap[DRAFT_STATE].draft)
})
expect(nextState).not.toBe(baseState)
expect(nextState.aMap).not.toBe(baseState.aMap)
Expand Down Expand Up @@ -780,7 +780,7 @@ function runBaseTest(name, useProxies, autoFreeze, useListener) {
const nextState = produce(baseState, s => {
// Set.prototype.set should return the Set itself
const res = s.aSet.add("force")
expect(res).toBe(s.aSet[DRAFT_STATE].draft)
if (!global.USES_BUILD) expect(res).toBe(s.aSet[DRAFT_STATE].draft)
})
expect(nextState).not.toBe(baseState)
expect(nextState.aSet).not.toBe(baseState.aSet)
Expand Down Expand Up @@ -904,29 +904,30 @@ function runBaseTest(name, useProxies, autoFreeze, useListener) {
})
})

it("preserves non-enumerable properties", () => {
const baseState = {}
// Non-enumerable object property
Object.defineProperty(baseState, "foo", {
value: {a: 1},
enumerable: false
})
// Non-enumerable primitive property
Object.defineProperty(baseState, "bar", {
value: 1,
enumerable: false
})
const nextState = produce(baseState, s => {
expect(s.foo).toBeTruthy()
expect(isEnumerable(s, "foo")).toBeFalsy()
s.bar++
expect(isEnumerable(s, "foo")).toBeFalsy()
s.foo.a++
expect(isEnumerable(s, "foo")).toBeFalsy()
if (!global.USES_BUILD)
it("preserves non-enumerable properties", () => {
const baseState = {}
// Non-enumerable object property
Object.defineProperty(baseState, "foo", {
value: {a: 1},
enumerable: false
})
// Non-enumerable primitive property
Object.defineProperty(baseState, "bar", {
value: 1,
enumerable: false
})
const nextState = produce(baseState, s => {
expect(s.foo).toBeTruthy()
expect(isEnumerable(s, "foo")).toBeFalsy()
s.bar++
expect(isEnumerable(s, "foo")).toBeFalsy()
s.foo.a++
expect(isEnumerable(s, "foo")).toBeFalsy()
})
expect(nextState.foo).toBeTruthy()
expect(isEnumerable(nextState, "foo")).toBeFalsy()
})
expect(nextState.foo).toBeTruthy()
expect(isEnumerable(nextState, "foo")).toBeFalsy()
})

it("throws on computed properties", () => {
const baseState = {}
Expand Down Expand Up @@ -1118,7 +1119,14 @@ function runBaseTest(name, useProxies, autoFreeze, useListener) {
produce(baseState, draft => {
draft.anArray[0] = 5
draft.anArray.unshift("test")
expect(enumerableOnly(draft.anArray)).toEqual(["test", 5, 2, {c: 3}, 1])
if (!global.USES_BUILD)
expect(enumerableOnly(draft.anArray)).toEqual([
"test",
5,
2,
{c: 3},
1
])
draft.stuffz = "coffee"
expect(draft.stuffz).toBe("coffee")
})
Expand Down Expand Up @@ -1727,7 +1735,7 @@ function runBaseTest(name, useProxies, autoFreeze, useListener) {
})

describe("base state type", () => {
testObjectTypes(produce)
if (!global.USES_BUILD) testObjectTypes(produce)
testLiteralTypes(produce)
})

Expand Down
4 changes: 3 additions & 1 deletion __tests__/empty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ describe("map set - proxy", () => {
const nextState = produce(baseState, s => {
// Map.prototype.set should return the Map itself
const res = s.aMap.set("force", true)
expect(res).toBe((s.aMap as any)[DRAFT_STATE].draft)
// @ts-ignore
if (!global.USES_BUILD)
expect(res).toBe((s.aMap as any)[DRAFT_STATE].draft)
})
expect(nextState).not.toBe(baseState)
expect(nextState.aMap).not.toBe(baseState.aMap)
Expand Down
46 changes: 36 additions & 10 deletions __tests__/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,17 @@ function createHookTests(useProxies) {
test("assign", () => {
const key1 = {prop: "val1"}
const key2 = {prop: "val2"}
produce(new Map([["a", 0], [key1, 1], [key2, 2]]), s => {
s.set("a", 10)
s.set(key1, 11)
})
produce(
new Map([
["a", 0],
[key1, 1],
[key2, 2]
]),
s => {
s.set("a", 10)
s.set(key1, 11)
}
)
expectCalls(onAssign)
})
test("assign (no change)", () => {
Expand All @@ -146,7 +153,19 @@ function createHookTests(useProxies) {
const key1 = {prop: "val1"}
produce(
new Map([
["a", new Map([[key1, new Map([["b", 1], ["c", 1], ["d", 1]])]])]
[
"a",
new Map([
[
key1,
new Map([
["b", 1],
["c", 1],
["d", 1]
])
]
])
]
]),
s => {
const nested = s.get("a").get(key1)
Expand Down Expand Up @@ -184,7 +203,7 @@ function createHookTests(useProxies) {
produce({a: new Set(["a", new Set([val1, 1])])}, s => {
let nested
s.a.forEach(value => {
if (isSet(value)) {
if (value instanceof Set) {
nested = value
}
})
Expand Down Expand Up @@ -245,10 +264,17 @@ function createHookTests(useProxies) {
test("delete", () => {
const key1 = {prop: "val1"}
const key2 = {prop: "val2"}
produce(new Map([["a", 0], [key1, 1], [key2, 2]]), s => {
s.delete("a")
s.delete(key1)
})
produce(
new Map([
["a", 0],
[key1, 1],
[key2, 2]
]),
s => {
s.delete("a")
s.delete(key1)
}
)
expectCalls(onDelete)
})
test("delete (no change)", () => {
Expand Down
58 changes: 32 additions & 26 deletions __tests__/polyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,41 @@ Symbol = SymbolConstructor
Object.assign = assign
Reflect.ownKeys = ownKeys

describe("Symbol", () => {
test("NOTHING", () => {
const value = common.NOTHING
expect(value).toBeTruthy()
expect(typeof value).toBe("object")
if (!global.USES_BUILD)
describe("Symbol", () => {
test("NOTHING", () => {
const value = common.NOTHING
expect(value).toBeTruthy()
expect(typeof value).toBe("object")
})
test("DRAFTABLE", () => {
const value = common.DRAFTABLE
expect(typeof value).toBe("string")
})
test("DRAFT_STATE", () => {
const value = common.DRAFT_STATE
expect(typeof value).toBe("string")
})
})
test("DRAFTABLE", () => {
const value = common.DRAFTABLE
expect(typeof value).toBe("string")
})
test("DRAFT_STATE", () => {
const value = common.DRAFT_STATE
expect(typeof value).toBe("string")
})
})

describe("Reflect.ownKeys", () => {
const {ownKeys} = common
if (!global.USES_BUILD)
describe("Reflect.ownKeys", () => {
const {ownKeys} = common

// Symbol keys are always last.
it("includes symbol keys", () => {
const s = SymbolConstructor()
const obj = {[s]: 1, b: 1}
expect(ownKeys(obj)).toEqual(["b", s])
})
// Symbol keys are always last.
it("includes symbol keys", () => {
const s = SymbolConstructor()
const obj = {[s]: 1, b: 1}
expect(ownKeys(obj)).toEqual(["b", s])
})

it("includes non-enumerable keys", () => {
const obj = {a: 1}
Object.defineProperty(obj, "b", {value: 1})
expect(ownKeys(obj)).toEqual(["a", "b"])
it("includes non-enumerable keys", () => {
const obj = {a: 1}
Object.defineProperty(obj, "b", {value: 1})
expect(ownKeys(obj)).toEqual(["a", "b"])
})
})

test("suppress jest warning", () => {
expect(true).toBe(true)
})
25 changes: 11 additions & 14 deletions __tests__/redux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,36 @@ import produce, {
} from "../src/"
import * as redux from "redux"

export interface State {
interface State {
counter: number
}

export interface Action {
interface Action {
type: string
payload: number
}

export const initialState: State = {
const initialState: State = {
counter: 0
}

/// =============== Actions

export function addToCounter(addNumber: number) {
function addToCounter(addNumber: number) {
return {
type: "ADD_TO_COUNTER",
payload: addNumber
}
}

export function subFromCounter(subNumber: number) {
function subFromCounter(subNumber: number) {
return {
type: "SUB_FROM_COUNTER",
payload: subNumber
}
}

export const reduceCounterProducer = (
state: State = initialState,
action: Action
) =>
const reduceCounterProducer = (state: State = initialState, action: Action) =>
produce(state, draftState => {
switch (action.type) {
case "ADD_TO_COUNTER":
Expand All @@ -53,7 +50,7 @@ export const reduceCounterProducer = (
}
})

export const reduceCounterCurriedProducer = produce(
const reduceCounterCurriedProducer = produce(
(draftState: Draft<State>, action: Action) => {
switch (action.type) {
case "ADD_TO_COUNTER":
Expand All @@ -69,11 +66,11 @@ export const reduceCounterCurriedProducer = produce(

/// =============== Reducers

export const reduce = redux.combineReducers({
const reduce = redux.combineReducers({
counterReducer: reduceCounterProducer
})

export const curredReduce = redux.combineReducers({
const curredReduce = redux.combineReducers({
counterReducer: reduceCounterCurriedProducer
})

Expand All @@ -82,8 +79,8 @@ export const curredReduce = redux.combineReducers({

// ================ store

export const store = redux.createStore(reduce)
export const curriedStore = redux.createStore(curredReduce)
const store = redux.createStore(reduce)
const curriedStore = redux.createStore(curredReduce)

it("#470 works with Redux combine reducers", () => {
assert(
Expand Down
17 changes: 17 additions & 0 deletions buid.jest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"moduleNameMapper": {
"src/.*": "<rootDir>"
},
"globals": {
"USES_BUILD": true,
"ts-jest": {
"tsConfig": {
"noUnusedLocals": false
}
}
},
"transform": {
"\\.js$": "babel-jest",
"\\.ts$": "ts-jest"
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
"react-native": "dist/immer.module.js",
"types": "./dist/index.d.ts",
"scripts": {
"test": "jest",
"test": "jest && yarn-or-npm test:build && yarn-or-npm test:flow",
"test:perf": "NODE_ENV=production yarn-or-npm build && cd __performance_tests__ && babel-node add-data.js && babel-node todo.js && babel-node incremental.js",
"test:flow": "yarn-or-npm flow check __tests__/flow",
"test:build": "yarn-or-npm build && yarn jest --config buid.jest.json",
"watch": "jest --watch",
"coverage": "jest --coverage",
"coveralls": "jest --coverage && cat ./coverage/lcov.info | ./node_modules/.bin/coveralls && rm -rf ./coverage",
Expand Down

0 comments on commit ef8990b

Please sign in to comment.