Skip to content

Commit

Permalink
test: autoExtension unit test (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
Timeless0911 authored Jul 15, 2024
1 parent b430742 commit a230db5
Show file tree
Hide file tree
Showing 15 changed files with 115 additions and 17 deletions.
16 changes: 8 additions & 8 deletions packages/core/tests/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { RslibConfig } from '../src/types/config';

describe('Should load config file correctly', () => {
test('Load config.js in cjs project', async () => {
const fixtureDir = join(__dirname, 'config/cjs');
const fixtureDir = join(__dirname, 'fixtures/config/cjs');
const configDir = join(fixtureDir, 'rslib.config.js');
const config = await loadConfig(configDir);
expect(config).toEqual({
Expand All @@ -22,7 +22,7 @@ describe('Should load config file correctly', () => {
});

test('Load config.mjs in cjs project', async () => {
const fixtureDir = join(__dirname, 'config/cjs');
const fixtureDir = join(__dirname, 'fixtures/config/cjs');
const configDir = join(fixtureDir, 'rslib.config.mjs');
const config = await loadConfig(configDir);
expect(config).toEqual({
Expand All @@ -39,7 +39,7 @@ describe('Should load config file correctly', () => {
});

test('Load config.ts in cjs project', async () => {
const fixtureDir = join(__dirname, 'config/cjs');
const fixtureDir = join(__dirname, 'fixtures/config/cjs');
const configDir = join(fixtureDir, 'rslib.config.ts');
const config = await loadConfig(configDir);
expect(config).toEqual({
Expand All @@ -56,7 +56,7 @@ describe('Should load config file correctly', () => {
});

test('Load config.cjs with defineConfig in cjs project', async () => {
const fixtureDir = join(__dirname, 'config/cjs');
const fixtureDir = join(__dirname, 'fixtures/config/cjs');
const configDir = join(fixtureDir, 'rslib.config.cjs');
const config = await loadConfig(configDir);
expect(config).toEqual({
Expand All @@ -73,7 +73,7 @@ describe('Should load config file correctly', () => {
});

test('Load config.js in esm project', async () => {
const fixtureDir = join(__dirname, 'config/esm');
const fixtureDir = join(__dirname, 'fixtures/config/esm');
const configDir = join(fixtureDir, 'rslib.config.js');
const config = await loadConfig(configDir);
expect(config).toEqual({
Expand All @@ -90,7 +90,7 @@ describe('Should load config file correctly', () => {
});

test('Load config.cjs in esm project', async () => {
const fixtureDir = join(__dirname, 'config/esm');
const fixtureDir = join(__dirname, 'fixtures/config/esm');
const configDir = join(fixtureDir, 'rslib.config.cjs');
const config = await loadConfig(configDir);
expect(config).toEqual({
Expand All @@ -107,7 +107,7 @@ describe('Should load config file correctly', () => {
});

test('Load config.ts in esm project', async () => {
const fixtureDir = join(__dirname, 'config/esm');
const fixtureDir = join(__dirname, 'fixtures/config/esm');
const configDir = join(fixtureDir, 'rslib.config.ts');
const config = await loadConfig(configDir);
expect(config).toEqual({
Expand All @@ -124,7 +124,7 @@ describe('Should load config file correctly', () => {
});

test('Load config.mjs with defineConfig in esm project', async () => {
const fixtureDir = join(__dirname, 'config/esm');
const fixtureDir = join(__dirname, 'fixtures/config/esm');
const configDir = join(fixtureDir, 'rslib.config.mjs');
const config = await loadConfig(configDir);
expect(config).toEqual({
Expand Down
7 changes: 0 additions & 7 deletions packages/core/tests/core.test.ts

This file was deleted.

99 changes: 99 additions & 0 deletions packages/core/tests/extension.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { join } from 'node:path';
import { describe, expect, it } from 'vitest';
import { getDefaultExtension } from '../src/utils/extension';

describe('should get extension correctly', () => {
it('autoExtension is false', () => {
const options = {
format: 'cjs',
root: '/path/to/root',
autoExtension: false,
};

const result = getDefaultExtension(options);

expect(result).toEqual({
jsExtension: '.js',
dtsExtension: '.d.ts',
});
});

it('package.json is broken', () => {
const options = {
format: 'cjs',
root: '/path/to/root',
autoExtension: true,
};

const result = getDefaultExtension(options);

expect(result).toEqual({
jsExtension: '.js',
dtsExtension: '.d.ts',
});
});

it('format is cjs and type is module in package.json', () => {
const options = {
format: 'cjs',
root: join(__dirname, 'fixtures/extension/type-module'),
autoExtension: true,
};

const result = getDefaultExtension(options);

expect(result).toEqual({
jsExtension: '.cjs',
dtsExtension: '.d.cts',
isModule: true,
});
});

it('format is cjs and type is commonjs in package.json', () => {
const options = {
format: 'cjs',
root: join(__dirname, 'fixtures/extension/type-commonjs'),
autoExtension: true,
};

const result = getDefaultExtension(options);

expect(result).toEqual({
jsExtension: '.js',
dtsExtension: '.d.ts',
isModule: false,
});
});

it('format is esm and type is commonjs in package.json', () => {
const options = {
format: 'esm',
root: join(__dirname, 'fixtures/extension/type-commonjs'),
autoExtension: true,
};

const result = getDefaultExtension(options);

expect(result).toEqual({
jsExtension: '.mjs',
dtsExtension: '.d.mts',
isModule: false,
});
});

it('format is esm and type is module in package.json', () => {
const options = {
format: 'esm',
root: join(__dirname, 'fixtures/extension/type-module'),
autoExtension: true,
};

const result = getDefaultExtension(options);

expect(result).toEqual({
jsExtension: '.js',
dtsExtension: '.d.ts',
isModule: true,
});
});
});
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { defineConfig } = require('../../../../core/src/config');
const { defineConfig } = require('../../../../../core/src/config');

module.exports = defineConfig((args) => ({
lib: [],
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineConfig } from '../../../../core/src/config';
import { defineConfig } from '../../../../../core/src/config';

export default defineConfig((args) => ({
lib: [],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}

0 comments on commit a230db5

Please sign in to comment.