Skip to content

Commit

Permalink
chore: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhulle committed Oct 6, 2023
1 parent 14f7c48 commit 6a3dd4f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
1 change: 0 additions & 1 deletion packages/core/mocks/hellowWorld.js

This file was deleted.

31 changes: 22 additions & 9 deletions packages/core/src/managers/fileManager/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { combineFiles, createFileSource, getIndexes } from './utils.ts'
import type { File } from './types.ts'

describe('FileManager utils', () => {
test.only('if getFileSource is returning code with imports', async () => {
test('if getFileSource is returning code with imports', async () => {
const code = createFileSource({
fileName: 'test.ts',
path: 'models/ts/test.ts',
Expand Down Expand Up @@ -147,11 +147,12 @@ export const test2 = 3;`,
])
})

test('if getFileSource is returning code with exports and exports as', () => {
test('if getFileSource is returning code with exports and exports as', async () => {
const fileImport: File = {
path: path.resolve('./src/models/file1.ts'),
fileName: 'file1.ts',
source: 'export const test = 2;',
source: `export const test = 2;
type Test = Pets | Lily | Dog;`,
imports: [
{
name: ['Pets'],
Expand Down Expand Up @@ -195,9 +196,21 @@ export const test2 = 3;`,
],
}

expect(createFileSource(fileImport)).toEqual('import type { Pets, Lily } from "./Pets";\nimport type Dog from "./Dog";\n\nexport const test = 2;')
expect(await format(createFileSource(fileImport))).toMatch(
await format(`
import type { Pets, Lily } from "./Pets";
import type Dog from "./Dog";
export const test = 2;
type Test = Pets | Lily | Dog;`),
)

expect(createFileSource(fileExport)).toEqual('export type { Pets, Lily } from "./Pets";\nexport type * as Dog from "./Dog";\n\n')
expect(await format(createFileSource(fileExport))).toEqual(
await format(`
export type { Pets, Lily } from "./Pets";
export type * as Dog from "./Dog";
`),
)
})

test('if combineFiles is combining `exports`, `imports` and `source` for the same file', () => {
Expand Down Expand Up @@ -333,7 +346,7 @@ export const test2 = 3;`,
const fileImportAdvanced: File = {
path: path.resolve('./src/models/file1.ts'),
fileName: 'file1.ts',
source: 'export const hello = process.env["HELLO"];',
source: 'export const hello = process.env["HELLO"]; type Test = Pets;',
imports: [
{
name: ['Pets'],
Expand All @@ -353,6 +366,7 @@ export const test2 = 3;`,
declare const TEST: string;
export const hello = typeof TEST !== 'undefined' ? TEST : undefined
type Test = Pets;
`,
imports: [
{
Expand All @@ -368,17 +382,15 @@ export const test2 = 3;`,

expect(await format(createFileSource(fileImport))).toEqual(
await format(`
import type { Pets } from "./Pets";
export const hello = "world";
`),
)
expect(await format(createFileSource(fileImportAdvanced))).toEqual(
await format(`
import type { Pets } from "./Pets";
export const hello = "world";
type Test = Pets;
`),
)
Expand All @@ -388,6 +400,7 @@ export const test2 = 3;`,
import type { Pets } from "./Pets";
export const hello = typeof "world" !== 'undefined' ? "world" : undefined
type Test = Pets;
`),
)
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/managers/fileManager/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,11 @@ export function combineExports(exports: Export[]): Export[] {
return exports.reduce((prev, curr) => {
const name = curr.name
const prevByPath = prev.findLast((imp) => imp.path === curr.path)
const uniquePrev = prev.findLast((imp) => imp.path === curr.path)
const uniquePrev = prev.findLast(
(imp) => imp.path === curr.path && isEqual(imp.name, name) && imp.isTypeOnly === curr.isTypeOnly && imp.asAlias === curr.asAlias,
)

if (uniquePrev || (Array.isArray(name) && !name.length)) {
if (uniquePrev || (Array.isArray(name) && !name.length) || (prevByPath?.asAlias && !curr.asAlias)) {
return prev
}

Check warning on line 117 in packages/core/src/managers/fileManager/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/managers/fileManager/utils.ts#L116-L117

Added lines #L116 - L117 were not covered by tests

Expand Down

0 comments on commit 6a3dd4f

Please sign in to comment.