Skip to content

Commit

Permalink
test: use inline snapshot instead of hard coding strings
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Dec 15, 2023
1 parent 35ab464 commit c1fd923
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/vercel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches:
- master
pull_request_target: null
pull_request: null

jobs:
deploy:
Expand Down
18 changes: 12 additions & 6 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ import {
generateTranslationRequestData,
generateSplitSentencesRequestData,
} from './generators.js'
import { API_URL, AUTO, SourceLanguage, TargetLanguage } from './settings.js'
import {
API_URL,
AUTO,
FormalityTone,
SourceLanguage,
TargetLanguage,
} from './settings.js'
import { abbreviateLanguage } from './utils.js'

const got = _got.extend({
Expand Down Expand Up @@ -72,7 +78,7 @@ export async function requestTranslation(
sourceLanguage: SourceLanguage,
identifier?: number,
alternatives?: number,
formalityTone?: 'formal' | 'informal',
formalityTone?: FormalityTone,
) {
const res = await splitSentences(text, sourceLanguage, identifier)
const data = generateTranslationRequestData(
Expand All @@ -96,31 +102,31 @@ export async function translate(
sourceLanguage?: SourceLanguage,
identifier?: number,
alternatives?: number,
formalityTone?: 'formal' | 'informal',
formalityTone?: FormalityTone,
): Promise<undefined>
export async function translate(
text: string,
targetLanguage: TargetLanguage,
sourceLanguage?: SourceLanguage,
identifier?: number,
alternatives?: number,
formalityTone?: 'formal' | 'informal',
formalityTone?: FormalityTone,
): Promise<string>
export async function translate(
text: string | null | undefined,
targetLanguage: TargetLanguage,
sourceLanguage?: SourceLanguage,
identifier?: number,
alternatives?: number,
formalityTone?: 'formal' | 'informal',
formalityTone?: FormalityTone,
): Promise<string | undefined>
export async function translate(
text: string | null | undefined,
targetLanguage: TargetLanguage,
sourceLanguage: SourceLanguage = AUTO,
identifier?: number,
alternatives?: number,
formalityTone?: 'formal' | 'informal',
formalityTone?: FormalityTone,
) {
text = text?.trim()
if (!text) {
Expand Down
8 changes: 4 additions & 4 deletions src/generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { generateId, generateTimestamp } from './hacks.js'
import {
AUTO,
SUPPORTED_FORMALITY_TONES,
Formality,
FormalityTone,
SourceLanguage,
TargetLanguage,
} from './settings.js'
Expand Down Expand Up @@ -45,13 +45,13 @@ export function generateJobs(sentences: string[], beams = 1) {
}))
}

function generateCommonJobParams(formality?: Formality) {
function generateCommonJobParams(formality?: FormalityTone) {
if (!formality) {
return {}
}

if (!SUPPORTED_FORMALITY_TONES.includes(formality)) {
throw new Error(`Formality tone ${formality} not supported.`)
throw new Error(`Formality tone \`${formality}\` not supported.`)
}

return { formality }
Expand All @@ -63,7 +63,7 @@ export function generateTranslationRequestData(
sentences: string[],
identifier = generateId(),
alternatives = 1,
formality?: Formality,
formality?: FormalityTone,
) {
return {
jsonrpc: '2.0',
Expand Down
2 changes: 1 addition & 1 deletion src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ export type SourceLanguage = TargetLanguage | 'auto'

export const SUPPORTED_FORMALITY_TONES = ['formal', 'informal'] as const

export type Formality = (typeof SUPPORTED_FORMALITY_TONES)[number]
export type FormalityTone = (typeof SUPPORTED_FORMALITY_TONES)[number]
18 changes: 9 additions & 9 deletions test/translation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,34 @@ test('translate russian', async () => {
const source_language = 'RU'
const target_language = 'EN'
const text = 'Я сошла с ума'
const expected_translation = "I'm out of my mind."
const translation = await translate(text, target_language, source_language)
expect(translation).toEqual(expected_translation)
expect(translation).toMatchInlineSnapshot(`"I've lost my mind"`)
})

test('translate chinese', async () => {
const source_language = 'ZH'
const target_language = 'dutch'
const text = '你好'
const expected_translation = 'Hallo'
const translation = await translate(text, target_language, source_language)
expect(translation).toContain(expected_translation)
expect(translation).toMatchInlineSnapshot(`"Hoe gaat het met je?"`)
})

test('translate greek romanian', async () => {
const source_language = 'gReEk'
const target_language = 'rO'
const text = 'Γεια σας'
const expected_translation = 'bună ziua'
// @ts-expect-error -- only upper, lower or capitalize case languages are supported in TypeScript
const translation = await translate(text, target_language, source_language)
expect((translation as string).toLowerCase()).toContain(expected_translation)
expect((translation as string).toLowerCase()).toMatchInlineSnapshot(
`"bună ziua."`,
)
})

test('translate sentence', async () => {
const text = 'Up and down.'
const expected_translation = 'Op en neer.'
expect(await translate(text, 'NL', 'EN')).toBe(expected_translation)
expect(await translate(text, 'NL', 'EN')).toMatchInlineSnapshot(
`"Op en neer."`,
)
})

test('translate sentences', async () => {
Expand Down Expand Up @@ -103,5 +103,5 @@ test('invalid_formal_tone', () =>
// @ts-expect-error
translate('ABC', 'DE', 'EN', undefined, undefined, 'politically incorrect'),
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Formality tone '{formality_tone}' not supported."`,
`[Error: Formality tone \`politically incorrect\` not supported.]`,
))
1 change: 1 addition & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default defineConfig({
test: {
coverage: {
include: ['src'],
exclude: ['src/cli.ts'],
provider: 'istanbul',
reporter: ['lcov', 'json', 'text'],
},
Expand Down

0 comments on commit c1fd923

Please sign in to comment.