Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
timofei-iatsenko committed Dec 23, 2024
1 parent d5d8c9c commit 7ccd129
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 211 deletions.
19 changes: 10 additions & 9 deletions packages/core/src/i18n.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { setupI18n } from "./i18n"
import { mockConsole, mockEnv } from "@lingui/jest-mocks"
import { compileMessage } from "@lingui/message-utils/compileMessage"
import { describe, it, expect, vi } from "vitest"

describe("I18n", () => {
describe("I18n.load", () => {
it("should emit event", () => {
const i18n = setupI18n()

const cbChange = jest.fn()
const cbChange = vi.fn()
i18n.on("change", cbChange)
i18n.load("en", { msg: "Message" })
expect(cbChange).toBeCalled()
Expand Down Expand Up @@ -66,7 +67,7 @@ describe("I18n", () => {
},
})

const cbChange = jest.fn()
const cbChange = vi.fn()
i18n.on("change", cbChange)
i18n.activate("en")
expect(cbChange).toBeCalled()
Expand Down Expand Up @@ -122,7 +123,7 @@ describe("I18n", () => {
})

mockEnv("production", () => {
jest.resetModules()
vi.resetModules()
mockConsole((console) => {
const { setupI18n } = require("@lingui/core")
const i18n = setupI18n()
Expand All @@ -137,7 +138,7 @@ describe("I18n", () => {
it("should set locale and messages", () => {
const i18n = setupI18n()

const cbChange = jest.fn()
const cbChange = vi.fn()
i18n.on("change", cbChange)

i18n.loadAndActivate({
Expand Down Expand Up @@ -364,7 +365,7 @@ describe("I18n", () => {
messages: { en: { exists: "exists" } },
})

const handler = jest.fn()
const handler = vi.fn()
i18n.on("missing", handler)
i18n._("exists")
expect(handler).toHaveBeenCalledTimes(0)
Expand All @@ -384,7 +385,7 @@ describe("I18n", () => {
messages: { en: {} },
})

const handler = jest.fn()
const handler = vi.fn()
i18n.on("missing", handler)
// @ts-expect-error 'id' should be of 'MessageDescriptor' or 'string' type.
i18n._()
Expand All @@ -407,7 +408,7 @@ describe("I18n", () => {
})

it("._ should call a function with message ID of missing translation", () => {
const missing = jest.fn((locale, id) => id.split("").reverse().join(""))
const missing = vi.fn((locale, id) => id.split("").reverse().join(""))
const i18n = setupI18n({
locale: "en",
messages: {
Expand Down Expand Up @@ -439,9 +440,9 @@ describe("I18n", () => {
"Text {0, plural, offset:1 =0 {No books} =1 {1 book} other {# books}}"
)
).toThrowErrorMatchingInlineSnapshot(`
"Lingui: Attempted to call a translation function without setting a locale.
[Error: Lingui: Attempted to call a translation function without setting a locale.
Make sure to call \`i18n.activate(locale)\` before using Lingui functions.
This issue may also occur due to a race condition in your initialization logic."
This issue may also occur due to a race condition in your initialization logic.]
`)
})
})
120 changes: 60 additions & 60 deletions packages/format-po-gettext/src/__snapshots__/po-gettext.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ exports[`po-gettext format > should convert gettext plurals to ICU plural messag
}
`;

exports[`po-gettext format using custom prefix handles custom prefix 1`] = `
msgid ""
exports[`po-gettext format > using custom prefix > handles custom prefix 1`] = `
"msgid ""
msgstr ""
"POT-Creation-Date: 2018-08-27 10:00+0000\\n"
"POT-Creation-Date: 2018-08-27 12:00+0200\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=utf-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
Expand All @@ -210,80 +210,80 @@ msgid "Singular case"
msgid_plural "Case number {anotherCount}"
msgstr[0] "Singular case"
msgstr[1] "Case number {anotherCount}"
"
`;

exports[`po-gettext format using custom prefix warns and falls back to using count if prefix is not found 1`] = `
exports[`po-gettext format > using custom prefix > warns and falls back to using count if prefix is not found 1`] = `
{
lO3l+X: {
comments: [
js-lingui:icu=%7BanotherCount%2C+plural%2C+one+%7BSingular+case%7D+other+%7BCase+number+%7BanotherCount%7D%7D%7D&pluralize_on=anotherCount,
"lO3l+X": {
"comments": [
"js-lingui:icu=%7BanotherCount%2C+plural%2C+one+%7BSingular+case%7D+other+%7BCase+number+%7BanotherCount%7D%7D%7D&pluralize_on=anotherCount",
],
context: null,
extra: {
flags: [],
translatorComments: [],
"context": null,
"extra": {
"flags": [],
"translatorComments": [],
},
message: Singular case,
obsolete: false,
origin: [],
translation: {count, plural, one {Singular case} other {Case number {anotherCount}}},
"message": "Singular case",
"obsolete": false,
"origin": [],
"translation": "{count, plural, one {Singular case} other {Case number {anotherCount}}}",
},
maCaRp: {
comments: [
js-lingui:icu=%7Bcount%2C+plural%2C+one+%7BSingular%7D+other+%7BPlural%7D%7D&pluralize_on=count,
"maCaRp": {
"comments": [
"js-lingui:icu=%7Bcount%2C+plural%2C+one+%7BSingular%7D+other+%7BPlural%7D%7D&pluralize_on=count",
],
context: null,
extra: {
flags: [],
translatorComments: [],
"context": null,
"extra": {
"flags": [],
"translatorComments": [],
},
message: Singular,
obsolete: false,
origin: [],
translation: ,
"message": "Singular",
"obsolete": false,
"origin": [],
"translation": "",
},
message_with_id: {
comments: [
js-lingui:pluralize_on=someCount,
js-lingui-explicit-id,
"message_with_id": {
"comments": [
"js-lingui:pluralize_on=someCount",
"js-lingui-explicit-id",
],
context: null,
extra: {
flags: [],
translatorComments: [],
"context": null,
"extra": {
"flags": [],
"translatorComments": [],
},
obsolete: false,
origin: [],
translation: {count, plural, one {Singular case} other {Case number {someCount}}},
"obsolete": false,
"origin": [],
"translation": "{count, plural, one {Singular case} other {Case number {someCount}}}",
},
message_with_id_but_without_translation: {
comments: [
Comment made by the developers.,
js-lingui:pluralize_on=count,
js-lingui-explicit-id,
"message_with_id_but_without_translation": {
"comments": [
"Comment made by the developers.",
"js-lingui:pluralize_on=count",
"js-lingui-explicit-id",
],
context: null,
extra: {
flags: [],
translatorComments: [],
"context": null,
"extra": {
"flags": [],
"translatorComments": [],
},
obsolete: false,
origin: [],
translation: ,
"obsolete": false,
"origin": [],
"translation": "",
},
static: {
comments: [
js-lingui-explicit-id,
"static": {
"comments": [
"js-lingui-explicit-id",
],
context: null,
extra: {
flags: [],
translatorComments: [],
"context": null,
"extra": {
"flags": [],
"translatorComments": [],
},
obsolete: false,
origin: [],
translation: Static message,
"obsolete": false,
"origin": [],
"translation": "Static message",
},
}
`;
42 changes: 21 additions & 21 deletions packages/format-po-gettext/src/po-gettext.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { afterAll, beforeAll, describe, expect, it } from "vitest"
import { afterAll, beforeAll, describe, expect, it, test } from "vitest"
import { mockConsole } from "@lingui/jest-mocks"
import fs from "fs"
import path from "path"
Expand Down Expand Up @@ -252,17 +252,17 @@ msgstr[2] "{count} jours"
// Note that the last case must be `other` (the 4th CLDR case name) instead of `many` (the 3rd CLDR case name).
expect(parsed).toMatchInlineSnapshot(`
{
ZETJEQ: {
comments: [],
context: null,
extra: {
flags: [],
translatorComments: [],
"ZETJEQ": {
"comments": [],
"context": null,
"extra": {
"flags": [],
"translatorComments": [],
},
message: {0, plural, one {{count} day} other {{count} days}},
obsolete: false,
origin: [],
translation: {0, plural, one {{count} jour} many {{count} jours} other {{count} jours}},
"message": "{0, plural, one {{count} day} other {{count} days}}",
"obsolete": false,
"origin": [],
"translation": "{0, plural, one {{count} jour} many {{count} jours} other {{count} jours}}",
},
}
`)
Expand Down Expand Up @@ -291,17 +291,17 @@ msgstr[3] "# dní"
// Note that the last case must be `other` (the 4th CLDR case name) instead of `many` (the 3rd CLDR case name).
expect(parsed).toMatchInlineSnapshot(`
{
GMnlGy: {
comments: [],
context: null,
extra: {
flags: [],
translatorComments: [],
"GMnlGy": {
"comments": [],
"context": null,
"extra": {
"flags": [],
"translatorComments": [],
},
message: {count, plural, one {{count} day} few {{count} days} many {{count} days} other {{count} days}},
obsolete: false,
origin: [],
translation: {#, plural, one {# den} few {# dny} other {# dní}},
"message": "{count, plural, one {{count} day} few {{count} days} many {{count} days} other {{count} days}}",
"obsolete": false,
"origin": [],
"translation": "{#, plural, one {# den} few {# dny} other {# dní}}",
},
}
`)
Expand Down
1 change: 1 addition & 0 deletions packages/loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
},
"devDependencies": {
"@lingui/format-json": "5.1.2",
"jiti": "^2.4.2",
"unbuild": "2.0.0",
"vite-tsconfig-paths": "^5.0.1",
"vitest": "^2.1.1",
Expand Down
49 changes: 0 additions & 49 deletions packages/loader/test/__snapshots__/loader.test.ts.snap

This file was deleted.

4 changes: 4 additions & 0 deletions packages/loader/test/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// const { createJiti } = require("jiti")
// const jiti = createJiti(__filename)

module.exports = require("../src/webpackLoader.ts")
3 changes: 2 additions & 1 deletion packages/loader/test/loader.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it, vi } from "vitest";
import { describe, expect, it, vi } from "vitest"
import path from "path"
import fs from "node:fs/promises"
import { build, watch } from "./compiler"
Expand All @@ -9,6 +9,7 @@ describe("lingui-loader", () => {
it("should compile catalog in po format", async () => {
const built = await build(path.join(__dirname, "po-format/entrypoint.js"))

console.log(built.stats.errors[0])
const data = await built.loadBundle()
expect(built.stats.errors).toEqual([])
expect(built.stats.warnings).toEqual([])
Expand Down
7 changes: 6 additions & 1 deletion packages/loader/vitest.config.mts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { defineConfig } from "vitest/config"
import tsconfigPaths from "vite-tsconfig-paths"
import commonjs from 'vite-plugin-commonjs'
import commonjs from "vite-plugin-commonjs"

export default defineConfig({
plugins: [commonjs(), tsconfigPaths({ projects: ["../../tsconfig.json"] })],
test: {
environment: "node",
server: {
deps: {
inline: ["webpack", "loader-runner"],
},
},
},
})
Loading

0 comments on commit 7ccd129

Please sign in to comment.