Skip to content

Commit

Permalink
test: improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
rdsedmundo committed Jun 19, 2021
1 parent 28ce573 commit 9ed0843
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 21 deletions.
@@ -1 +1 @@
export const something = true;
export const something = () => true;
@@ -0,0 +1,3 @@
{
"include": []
}
87 changes: 67 additions & 20 deletions packages/typescript-estree/tests/lib/parse.test.ts
Expand Up @@ -679,38 +679,85 @@ describe('parseAndGenerateServices', () => {
const PROJECT_DIR = resolve(FIXTURES_DIR, '../moduleResolver');
const code = `
import { something } from '__PLACEHOLDER__';
something;
something();
`;
const config: TSESTreeOptions = {
comment: true,
tokens: true,
range: true,
loc: true,
tsconfigRootDir: PROJECT_DIR,
project: './tsconfig.json',
tsconfigRootDir: PROJECT_DIR,
filePath: resolve(PROJECT_DIR, 'file.ts'),
};
const withDefaultProgramConfig: TSESTreeOptions = {
...config,
project: './tsconfig.defaultProgram.json',
createDefaultProgram: true,
};

it('returns error because __PLACEHOLDER__ can not be resolved', () => {
expect(
parser
.parseAndGenerateServices(code, config)
.services.program.getSemanticDiagnostics(),
).toHaveProperty(
[0, 'messageText'],
"Cannot find module '__PLACEHOLDER__' or its corresponding type declarations.",
);
});
describe('when file is in the project', () => {
it('returns error if __PLACEHOLDER__ can not be resolved', () => {
expect(
parser
.parseAndGenerateServices(code, config)
.services.program.getSemanticDiagnostics(),
).toHaveProperty(
[0, 'messageText'],
"Cannot find module '__PLACEHOLDER__' or its corresponding type declarations.",
);
});

it('resolves __PLACEHOLDER__ correctly', () => {
expect(
parser
.parseAndGenerateServices(code, {
it('throws error if moduleResolver can not be found', () => {
expect(() =>
parser.parseAndGenerateServices(code, {
...config,
moduleResolver: resolve(PROJECT_DIR, './moduleResolver.js'),
})
.services.program.getSemanticDiagnostics(),
).toHaveLength(0);
moduleResolver: resolve(
PROJECT_DIR,
'./this_moduleResolver_does_not_exist.js',
),
}),
).toThrowErrorMatchingInlineSnapshot(`
"Could not find the provided parserOptions.moduleResolver.
Hint: use an absolute path if you are not in control over where the ESLint instance runs."
`);
});

it('resolves __PLACEHOLDER__ correctly', () => {
expect(
parser
.parseAndGenerateServices(code, {
...config,
moduleResolver: resolve(PROJECT_DIR, './moduleResolver.js'),
})
.services.program.getSemanticDiagnostics(),
).toHaveLength(0);
});
});

describe('when file is not in the project and createDefaultProgram=true', () => {
it('returns error because __PLACEHOLDER__ can not be resolved', () => {
expect(
parser
.parseAndGenerateServices(code, withDefaultProgramConfig)
.services.program.getSemanticDiagnostics(),
).toHaveProperty(
[0, 'messageText'],
"Cannot find module '__PLACEHOLDER__' or its corresponding type declarations.",
);
});

it('resolves __PLACEHOLDER__ correctly', () => {
expect(
parser
.parseAndGenerateServices(code, {
...withDefaultProgramConfig,
moduleResolver: resolve(PROJECT_DIR, './moduleResolver.js'),
})
.services.program.getSemanticDiagnostics(),
).toHaveLength(0);
});
});
});
});

0 comments on commit 9ed0843

Please sign in to comment.