Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(gatsby): fix tests setup for queries after using babel-plugin-lodash #36015

Merged
merged 1 commit into from
Jun 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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