Skip to content

Commit

Permalink
test(gatsby): fix tests setup for queries after using babel-plugin-lo…
Browse files Browse the repository at this point in the history
…dash (#36015)
  • Loading branch information
pieh committed Jun 30, 2022
1 parent 96c28bf commit d436866
Showing 1 changed file with 33 additions and 25 deletions.
58 changes: 33 additions & 25 deletions packages/gatsby/src/schema/__tests__/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const withResolverContext = require(`../context`)
jest.mock(`../../utils/api-runner-node`)
const apiRunnerNode = require(`../../utils/api-runner-node`)

const mockActualOrderBy = jest.requireActual(`lodash`).orderBy
jest.mock(`lodash/orderBy`, () => jest.fn(mockActualOrderBy))
const orderBySpy = require(`lodash/orderBy`)

const getTestNodes = require(`./fixtures/queries`)

const { getDataStore, getNode } = require(`../../datastore`)
Expand Down Expand Up @@ -34,6 +38,10 @@ jest.mock(`gatsby-cli/lib/reporter`, () => {
}
})

beforeEach(() => {
orderBySpy.mockClear()
})

describe(`Query schema`, () => {
let schema
let schemaComposer
Expand Down Expand Up @@ -1216,9 +1224,7 @@ describe(`Query schema`, () => {

const { clearKeptObjects } = require(`lmdb`)

const actualOrderBy = jest.requireActual(`lodash`).orderBy
const spy = jest.spyOn(require(`lodash`), `orderBy`)
spy.mockImplementationOnce((...args) => {
orderBySpy.mockImplementationOnce((...args) => {
// eslint thinks that WeakRef is not defined for some reason :shrug:
// eslint-disable-next-line no-undef
const weakNode = new WeakRef(getNode(`md1`))
Expand All @@ -1239,9 +1245,11 @@ describe(`Query schema`, () => {
)
}

return actualOrderBy(...args)
return mockActualOrderBy(...args)
})

expect(orderBySpy).not.toBeCalled()

// sort added only to hit code path using `orderBy`,
// which we use to force GC to simulate node "randomly"
// releasing nodes from memory as they shouldn't be strongly
Expand All @@ -1266,7 +1274,7 @@ describe(`Query schema`, () => {
const results = await runQuery(query)

// make sure we released all nodes from memory in middle of the run
expect(spy).toBeCalled()
expect(orderBySpy).toBeCalled()

const expected = {
allMarkdown: {
Expand Down Expand Up @@ -1403,9 +1411,7 @@ describe(`Query schema`, () => {

const { clearKeptObjects } = require(`lmdb`)

const actualOrderBy = jest.requireActual(`lodash`).orderBy
const spy = jest.spyOn(require(`lodash`), `orderBy`)
spy.mockImplementationOnce((...args) => {
orderBySpy.mockImplementationOnce((...args) => {
// eslint thinks that WeakRef is not defined for some reason :shrug:
// eslint-disable-next-line no-undef
const weakNode = new WeakRef(getNode(`md1`))
Expand All @@ -1426,9 +1432,11 @@ describe(`Query schema`, () => {
)
}

return actualOrderBy(...args)
return mockActualOrderBy(...args)
})

expect(orderBySpy).not.toBeCalled()

// sort added only to hit code path using `orderBy`,
// which we use to force GC to simulate node "randomly"
// releasing nodes from memory as they shouldn't be strongly
Expand All @@ -1443,7 +1451,7 @@ describe(`Query schema`, () => {
const results = await runQuery(query)

// make sure we released all nodes from memory in middle of the run
expect(spy).toBeCalled()
expect(orderBySpy).toBeCalled()

const expected = {
allMarkdown: {
Expand Down Expand Up @@ -1505,9 +1513,7 @@ describe(`Query schema`, () => {

const { clearKeptObjects } = require(`lmdb`)

const actualOrderBy = jest.requireActual(`lodash`).orderBy
const spy = jest.spyOn(require(`lodash`), `orderBy`)
spy.mockImplementationOnce((...args) => {
orderBySpy.mockImplementationOnce((...args) => {
// eslint thinks that WeakRef is not defined for some reason :shrug:
// eslint-disable-next-line no-undef
const weakNode = new WeakRef(getNode(`md1`))
Expand All @@ -1528,9 +1534,11 @@ describe(`Query schema`, () => {
)
}

return actualOrderBy(...args)
return mockActualOrderBy(...args)
})

expect(orderBySpy).not.toBeCalled()

// sort added only to hit code path using `orderBy`,
// which we use to force GC to simulate node "randomly"
// releasing nodes from memory as they shouldn't be strongly
Expand All @@ -1545,7 +1553,7 @@ describe(`Query schema`, () => {
const results = await runQuery(query)

// make sure we released all nodes from memory in middle of the run
expect(spy).toBeCalled()
expect(orderBySpy).toBeCalled()

expect(results.errors).toBeUndefined()
expect(results.data.allMarkdown.max).toEqual(399)
Expand Down Expand Up @@ -1601,9 +1609,7 @@ describe(`Query schema`, () => {

const { clearKeptObjects } = require(`lmdb`)

const actualOrderBy = jest.requireActual(`lodash`).orderBy
const spy = jest.spyOn(require(`lodash`), `orderBy`)
spy.mockImplementationOnce((...args) => {
orderBySpy.mockImplementationOnce((...args) => {
// eslint thinks that WeakRef is not defined for some reason :shrug:
// eslint-disable-next-line no-undef
const weakNode = new WeakRef(getNode(`md1`))
Expand All @@ -1624,9 +1630,11 @@ describe(`Query schema`, () => {
)
}

return actualOrderBy(...args)
return mockActualOrderBy(...args)
})

expect(orderBySpy).not.toBeCalled()

// sort added only to hit code path using `orderBy`,
// which we use to force GC to simulate node "randomly"
// releasing nodes from memory as they shouldn't be strongly
Expand All @@ -1641,7 +1649,7 @@ describe(`Query schema`, () => {
const results = await runQuery(query)

// make sure we released all nodes from memory in middle of the run
expect(spy).toBeCalled()
expect(orderBySpy).toBeCalled()

expect(results.errors).toBeUndefined()
expect(results.data.allMarkdown.min).toEqual(199)
Expand Down Expand Up @@ -1696,9 +1704,7 @@ describe(`Query schema`, () => {

const { clearKeptObjects } = require(`lmdb`)

const actualOrderBy = jest.requireActual(`lodash`).orderBy
const spy = jest.spyOn(require(`lodash`), `orderBy`)
spy.mockImplementationOnce((...args) => {
orderBySpy.mockImplementationOnce((...args) => {
// eslint thinks that WeakRef is not defined for some reason :shrug:
// eslint-disable-next-line no-undef
const weakNode = new WeakRef(getNode(`md1`))
Expand All @@ -1719,9 +1725,11 @@ describe(`Query schema`, () => {
)
}

return actualOrderBy(...args)
return mockActualOrderBy(...args)
})

expect(orderBySpy).not.toBeCalled()

// sort added only to hit code path using `orderBy`,
// which we use to force GC to simulate node "randomly"
// releasing nodes from memory as they shouldn't be strongly
Expand All @@ -1736,7 +1744,7 @@ describe(`Query schema`, () => {
const results = await runQuery(query)

// make sure we released all nodes from memory in middle of the run
expect(spy).toBeCalled()
expect(orderBySpy).toBeCalled()

expect(results.errors).toBeUndefined()
expect(results.data.allMarkdown.sum).toEqual(199 + 399)
Expand Down

0 comments on commit d436866

Please sign in to comment.