Skip to content

Commit

Permalink
refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yuheiy committed Oct 6, 2024
1 parent e4731b3 commit aa474c8
Showing 1 changed file with 106 additions and 155 deletions.
261 changes: 106 additions & 155 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,180 +2,131 @@ import { describe, expect, test } from 'vitest';
import { scanImportDeclarations } from '../src/index.js';

describe('scanImportDeclarations', () => {
test('general props', () => {
const actual = scanImportDeclarations(
'test/__fixtures__/default-import.ts',
);
expect(
actual.map(({ details, ...generalProps }) => generalProps),
).toStrictEqual([
{
filePath: 'test/__fixtures__/default-import.ts',
position: {
end: 42,
start: 15,
},
line: {
end: 2,
start: 2,
},
statement: "import m1 from 'my-module';",
moduleSpecifierValue: 'my-module',
},
{
filePath: 'test/__fixtures__/default-import.ts',
position: {
end: 75,
start: 43,
},
line: {
end: 3,
start: 3,
},
statement: "import type m2 from 'my-module';",
moduleSpecifierValue: 'my-module',
},
]);
});

test('default import', () => {
const actual = scanImportDeclarations(
'test/__fixtures__/default-import.ts',
);
expect(actual).toMatchInlineSnapshot(`
[
{
"details": {
"importedBinding": "m1",
"isTypeOnly": false,
"type": "default_import",
},
"filePath": "test/__fixtures__/default-import.ts",
"line": {
"end": 2,
"start": 2,
},
"moduleSpecifierValue": "my-module",
"position": {
"end": 42,
"start": 15,
},
"statement": "import m1 from 'my-module';",
},
{
"details": {
"importedBinding": "m2",
"isTypeOnly": true,
"type": "default_import",
},
"filePath": "test/__fixtures__/default-import.ts",
"line": {
"end": 3,
"start": 3,
},
"moduleSpecifierValue": "my-module",
"position": {
"end": 75,
"start": 43,
},
"statement": "import type m2 from 'my-module';",
},
]
`);
expect(actual.map(({ details }) => ({ details }))).toStrictEqual([
{
details: {
type: 'default_import',
isTypeOnly: false,
importedBinding: 'm1',
},
},
{
details: {
type: 'default_import',
isTypeOnly: true,
importedBinding: 'm2',
},
},
]);
});

test('namespace import', () => {
const actual = scanImportDeclarations(
'test/__fixtures__/namespace-import.ts',
);
expect(actual).toMatchInlineSnapshot(`
[
{
"details": {
"importedBinding": "m1",
"isTypeOnly": false,
"type": "namespace_import",
},
"filePath": "test/__fixtures__/namespace-import.ts",
"line": {
"end": 2,
"start": 2,
},
"moduleSpecifierValue": "my-module",
"position": {
"end": 47,
"start": 15,
},
"statement": "import * as m1 from 'my-module';",
},
{
"details": {
"importedBinding": "m2",
"isTypeOnly": true,
"type": "namespace_import",
},
"filePath": "test/__fixtures__/namespace-import.ts",
"line": {
"end": 3,
"start": 3,
},
"moduleSpecifierValue": "my-module",
"position": {
"end": 85,
"start": 48,
},
"statement": "import type * as m2 from 'my-module';",
},
]
`);
expect(actual.map(({ details }) => ({ details }))).toStrictEqual([
{
details: {
type: 'namespace_import',
isTypeOnly: false,
importedBinding: 'm1',
},
},
{
details: {
type: 'namespace_import',
isTypeOnly: true,
importedBinding: 'm2',
},
},
]);
});

test('named imports', () => {
const actual = scanImportDeclarations('test/__fixtures__/named-imports.ts');
expect(actual).toMatchInlineSnapshot(`
[
{
"details": {
"elements": [
{
"importedBinding": "m1",
"isTypeOnly": false,
"moduleExportName": "m1",
},
{
"importedBinding": "m2",
"isTypeOnly": false,
"moduleExportName": "m2",
},
],
"type": "named_imports",
},
"filePath": "test/__fixtures__/named-imports.ts",
"line": {
"end": 2,
"start": 2,
},
"moduleSpecifierValue": "my-module",
"position": {
"end": 50,
"start": 15,
},
"statement": "import { m1, m2 } from 'my-module';",
},
{
"details": {
"elements": [
{
"importedBinding": "m3",
"isTypeOnly": false,
"moduleExportName": "m3",
},
],
"type": "named_imports",
},
"filePath": "test/__fixtures__/named-imports.ts",
"line": {
"end": 3,
"start": 3,
},
"moduleSpecifierValue": "my-module",
"position": {
"end": 87,
"start": 51,
},
"statement": "import type { m3 } from 'my-module';",
},
]
`);
expect(actual.map(({ details }) => ({ details }))).toStrictEqual([
{
details: {
type: 'named_imports',
elements: [
{
isTypeOnly: false,
importedBinding: 'm1',
moduleExportName: 'm1',
},
{
isTypeOnly: false,
importedBinding: 'm2',
moduleExportName: 'm2',
},
],
},
},
{
details: {
type: 'named_imports',
elements: [
{
isTypeOnly: false,
importedBinding: 'm3',
moduleExportName: 'm3',
},
],
},
},
]);
});

test('side effect import', () => {
const actual = scanImportDeclarations(
'test/__fixtures__/side-effect-import.ts',
);
expect(actual).toMatchInlineSnapshot(`
[
{
"details": {
"type": "side_effect_import",
},
"filePath": "test/__fixtures__/side-effect-import.ts",
"line": {
"end": 2,
"start": 2,
},
"moduleSpecifierValue": "my-module",
"position": {
"end": 34,
"start": 15,
},
"statement": "import 'my-module';",
},
]
`);
expect(actual.map(({ details }) => ({ details }))).toStrictEqual([
{
details: {
type: 'side_effect_import',
},
},
]);
});
});

0 comments on commit aa474c8

Please sign in to comment.