Skip to content

Commit

Permalink
test(): fix few broken test cases, and improve noisy tests (#1456)
Browse files Browse the repository at this point in the history
  • Loading branch information
timofei-iatsenko authored Feb 22, 2023
1 parent cb3548d commit 3107902
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ Array [
message: undefined,
origin: Array [
js-with-macros.js,
34,
28,
],
},
Object {
Expand All @@ -224,7 +224,7 @@ Array [
message: undefined,
origin: Array [
js-with-macros.js,
39,
33,
],
},
Object {
Expand All @@ -234,7 +234,7 @@ Array [
message: undefined,
origin: Array [
js-with-macros.js,
44,
38,
],
},
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ const withTIdBacktick = t({
id: `Backtick`
})

const id = 'message id'

const withUnknownId = t({
id: id
})

const tWithContextA = t({
id: "Some ID",
context: "Context1"
Expand All @@ -44,4 +38,4 @@ const tWithContextB = t({
const defineMessageWithContext = defineMessage({
id: "Some ID",
context: "Context2"
})
})
130 changes: 91 additions & 39 deletions packages/babel-plugin-extract-messages/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,43 +62,56 @@ const transformCode = (
return messages
}

function expectNoConsole(cb: () => void) {
return mockConsole((console) => {
cb()

expect(console.warn).not.toBeCalled()
expect(console.error).not.toBeCalled()
})
}

describe("@lingui/babel-plugin-extract-messages", function () {
it("should ignore files without lingui import", () => {
const messages = transform("without-lingui.js")
expect(messages.length).toBe(0)
expectNoConsole(() => {
const messages = transform("without-lingui.js")
expect(messages.length).toBe(0)
})
})

it("should extract all messages from JSX files", () => {
const messages = transform("jsx-without-macros.js")
expect(messages).toMatchSnapshot()
expectNoConsole(() => {
const messages = transform("jsx-without-macros.js")
expect(messages).toMatchSnapshot()
})
})

describe("JSX", () => {
it("Should not rise warning when translation from variable", () => {
const code = `
import { Trans } from "@lingui/react"
import { Trans } from "@lingui/react";
;<Trans id={message} />
<Trans id={message} />;
`
mockConsole((console) => {
expectNoConsole(() => {
const messages = transformCode(code)

expect(messages.length).toBe(0)
expect(console.warn).not.toBeCalled()
})
})

it("Should log error when no ID provided", () => {
const code = `
import { Trans } from "@lingui/react"
import { Trans } from "@lingui/react";
;<Trans />
;<Trans message="Missing ID" />
<Trans />;
<Trans message="Missing ID" />;
`
mockConsole((console) => {
const messages = transformCode(code)

expect(messages.length).toBe(0)
expect(console.error).not.toBeCalled()
expect(console.warn).toBeCalledWith(
expect.stringContaining(`Missing message ID`)
)
Expand All @@ -108,8 +121,10 @@ import { Trans } from "@lingui/react"

describe("CallExpression i18n._()", () => {
it("should extract messages from i18n._ call expressions", () => {
const messages = transform("js-call-expression.js")
expect(messages).toMatchSnapshot()
expectNoConsole(() => {
const messages = transform("js-call-expression.js")
expect(messages).toMatchSnapshot()
})
})

it("Should log error when no ID provided", () => {
Expand All @@ -119,6 +134,7 @@ import { Trans } from "@lingui/react"
const messages = transformCode(code)

expect(messages.length).toBe(0)
expect(console.error).not.toBeCalled()
expect(console.warn).toBeCalledWith(
expect.stringContaining(`Missing message ID`)
)
Expand All @@ -128,11 +144,9 @@ import { Trans } from "@lingui/react"
it("Should not rise warning when translation from variable", () => {
const code = `i18n._(message)`

mockConsole((console) => {
expectNoConsole(() => {
const messages = transformCode(code)

expect(messages.length).toBe(0)
expect(console.warn).not.toBeCalled()
})
})

Expand All @@ -142,6 +156,7 @@ import { Trans } from "@lingui/react"
return mockConsole((console) => {
transformCode(code)

expect(console.error).not.toBeCalled()
expect(console.warn).toBeCalledWith(
expect.stringContaining(
"Only strings or template literals could be extracted."
Expand All @@ -152,10 +167,12 @@ import { Trans } from "@lingui/react"

it("Should support string concatenation", () => {
const code = `const msg = i18n._('message.id', {}, {comment: "first " + "second " + "third"})`
const messages = transformCode(code)

expect(messages[0]).toMatchObject({
comment: "first second third",
expectNoConsole(() => {
const messages = transformCode(code)
expect(messages[0]).toMatchObject({
comment: "first second third",
})
})
})

Expand All @@ -170,22 +187,23 @@ import { Trans } from "@lingui/react"
},
});
`

return mockConsole((console) => {
expectNoConsole(() => {
const messages = transformCode(code)
expect(messages.length).toBe(1)
expect(console.warn).not.toBeCalled()
})
})
})

describe("StringLiteral", () => {
it("Should extract from marked StringLiteral", () => {
const code = `const t = /*i18n*/'Message'`
const messages = transformCode(code)

expect(messages[0]).toMatchObject({
id: "Message",
expectNoConsole(() => {
const messages = transformCode(code)

expect(messages[0]).toMatchObject({
id: "Message",
})
})
})

Expand All @@ -196,6 +214,7 @@ import { Trans } from "@lingui/react"
const messages = transformCode(code)

expect(messages.length).toBe(0)
expect(console.error).not.toBeCalled()
expect(console.warn).toBeCalledWith(
expect.stringContaining(`Empty StringLiteral`)
)
Expand All @@ -205,16 +224,21 @@ import { Trans } from "@lingui/react"

describe("MessageDescriptor", () => {
it("should extract messages from MessageDescriptors", () => {
const messages = transform("js-message-descriptor.js")
expect(messages).toMatchSnapshot()
expectNoConsole(() => {
const messages = transform("js-message-descriptor.js")
expect(messages).toMatchSnapshot()
})
})

it("Should extract id from TemplateLiteral", () => {
const code = "const msg = /*i18n*/{id: `Message`}"
const messages = transformCode(code)

expect(messages[0]).toMatchObject({
id: "Message",
expectNoConsole(() => {
const messages = transformCode(code)

expect(messages[0]).toMatchObject({
id: "Message",
})
})
})

Expand All @@ -225,6 +249,8 @@ import { Trans } from "@lingui/react"
const messages = transformCode(code)

expect(messages.length).toBe(0)

expect(console.error).not.toBeCalled()
expect(console.warn).toBeCalledWith(
expect.stringContaining(
`Could not extract from template literal with expressions.`
Expand All @@ -240,18 +266,36 @@ import { Trans } from "@lingui/react"
const messages = transformCode(code)

expect(messages.length).toBe(0)
expect(console.error).not.toBeCalled()

expect(console.warn).toBeCalledWith(
expect.stringContaining(`Missing message ID`)
)
})
})

it("Should log error when not a string provided as ID", () => {
const code = "const msg = /*i18n*/ {id: id}"

return mockConsole((console) => {
transformCode(code)

expect(console.error).not.toBeCalled()
expect(console.warn).toBeCalledWith(
expect.stringContaining(
"Only strings or template literals could be extracted."
)
)
})
})

it("Should log error when not a string provided as comment", () => {
const code = `const msg = /*i18n*/ {id: "msg.id", comment: variable}`

return mockConsole((console) => {
transformCode(code)

expect(console.error).not.toBeCalled()
expect(console.warn).toBeCalledWith(
expect.stringContaining(
"Only strings or template literals could be extracted."
Expand All @@ -262,26 +306,34 @@ import { Trans } from "@lingui/react"

it("Should support string concatenation in comment", () => {
const code = `const msg = /*i18n*/ {id: "msg.id", comment: "first " + "second " + "third"}`
const messages = transformCode(code)

expect(messages[0]).toMatchObject({
comment: "first second third",
return expectNoConsole(() => {
const messages = transformCode(code)
expect(messages[0]).toMatchObject({
comment: "first second third",
})
})
})
})

it("should extract all messages from JSX files (macros)", () => {
const messages = transform("jsx-with-macros.js")
expect(messages).toMatchSnapshot()
return expectNoConsole(() => {
const messages = transform("jsx-with-macros.js")
expect(messages).toMatchSnapshot()
})
})

it("should extract Plural messages from JSX files when there's no Trans tag (integration)", () => {
const messages = transform("jsx-without-trans.js")
expect(messages).toMatchSnapshot()
return expectNoConsole(() => {
const messages = transform("jsx-without-trans.js")
expect(messages).toMatchSnapshot()
})
})

it("should extract all messages from JS files (macros)", () => {
const messages = transform("js-with-macros.js")
expect(messages).toMatchSnapshot()
return expectNoConsole(() => {
const messages = transform("js-with-macros.js")
expect(messages).toMatchSnapshot()
})
})
})
18 changes: 10 additions & 8 deletions packages/cli/src/api/catalog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
order,
normalizeRelativePath,
CatalogProps,
AllCatalogsType,
} from "./catalog"
import { createCompiledCatalog } from "./compile"

Expand Down Expand Up @@ -257,8 +258,8 @@ describe("Catalog", () => {
},
mockConfig()
)

mockConsole(async (console) => {
expect.assertions(1)
await mockConsole(async (console) => {
await catalog.collect({
files: [fixture("duplicate-id.js")],
})
Expand All @@ -271,7 +272,7 @@ describe("Catalog", () => {
})
})

it("should handle errors", () => {
it("should handle syntax errors", async () => {
const catalog = new Catalog(
{
name: "messages",
Expand All @@ -282,12 +283,13 @@ describe("Catalog", () => {
mockConfig()
)

mockConsole(async (console) => {
expect.assertions(2)
await mockConsole(async (console) => {
const messages = await catalog.collect()
expect(console.error).toBeCalledWith(
expect.stringContaining(`Cannot process file`)
)
expect(messages).toMatchSnapshot()
expect(messages).toBeFalsy()
})
})
})
Expand Down Expand Up @@ -316,7 +318,7 @@ describe("Catalog", () => {
*/

it("should initialize catalog", () => {
const prevCatalogs = { en: null, cs: null }
const prevCatalogs: AllCatalogsType = { en: null, cs: null }
const nextCatalog = {
"custom.id": makeNextMessage({
message: "Message with custom ID",
Expand Down Expand Up @@ -843,7 +845,7 @@ describe("getCatalogs", () => {
)
).toThrowErrorMatchingSnapshot()

// Use valus from config in error message
// Use values from config in error message
expect(() =>
getCatalogs(
mockConfig({
Expand Down Expand Up @@ -1032,7 +1034,7 @@ describe("normalizeRelativePath", () => {
)
})

it("directories without ending slash are correctly treaten as dirs", () => {
it("directories without ending slash are correctly treated as dirs", () => {
mockFs({
componentA: {
"index.js": mockFs.file(),
Expand Down
Loading

0 comments on commit 3107902

Please sign in to comment.