-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(gls-utils): #1055 - move file test, refactor for fixtures
- Loading branch information
Showing
11 changed files
with
167 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,8 @@ | |
}, | ||
"license": "MIT", | ||
"files": [ | ||
"dist" | ||
"dist", | ||
"esm" | ||
], | ||
"keywords": [ | ||
"graphql" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,8 @@ | |
}, | ||
"license": "MIT", | ||
"files": [ | ||
"dist" | ||
"dist", | ||
"esm" | ||
], | ||
"keywords": [ | ||
"graphql" | ||
|
3 changes: 3 additions & 0 deletions
3
packages/graphql-language-service-utils/src/__tests__/__fixtures__/file.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
example: true, | ||
}; |
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions
3
packages/graphql-language-service-utils/src/__tests__/__fixtures__/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "example" | ||
} |
133 changes: 133 additions & 0 deletions
133
packages/graphql-language-service-utils/src/__tests__/file-test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import path from 'path'; | ||
|
||
import { | ||
getFileExtension, | ||
getPathWithoutExtension, | ||
resolveFile, | ||
requireFile, | ||
} from '../file'; | ||
|
||
describe('getFileExtension', () => { | ||
it('should resolve an extension', () => { | ||
const extension = getFileExtension('example/example.txt'); | ||
expect(extension).toEqual('txt'); | ||
}); | ||
it('should resolve null when no extension is present', () => { | ||
const extension = getFileExtension('example/example'); | ||
expect(extension).toEqual(null); | ||
}); | ||
|
||
it('should return an extension with multiple dots in the path', () => { | ||
const extension = getFileExtension( | ||
'example.example/example/something.esm.js', | ||
); | ||
expect(extension).toEqual('js'); | ||
}); | ||
}); | ||
|
||
describe('getPathWithoutExtension', () => { | ||
it('should resolve when path has extension', () => { | ||
const extension = getPathWithoutExtension('example/example.txt', 'txt'); | ||
expect(extension).toEqual('example/example'); | ||
}); | ||
it('should resolve when path has no extension', () => { | ||
const extension = getPathWithoutExtension( | ||
'example/example.example/example', | ||
null, | ||
); | ||
expect(extension).toEqual('example/example.example/example'); | ||
}); | ||
}); | ||
|
||
describe('requireFile', () => { | ||
it('should require file with extension using json', async () => { | ||
const file = await requireFile( | ||
path.join(__dirname, '__fixtures__', 'package.json'), | ||
); | ||
expect(file.name).toEqual('example'); | ||
}); | ||
it('should require file with extension using js', async () => { | ||
const file = await requireFile( | ||
path.join(__dirname, '__fixtures__', 'file.js'), | ||
); | ||
expect(file.example).toEqual(true); | ||
}); | ||
it('should fail when requiring an invalid extension', () => { | ||
expect(() => requireFile('./__fixtures__/invalid.fake')).toThrowError( | ||
`cannot import() module with extension 'fake'`, | ||
); | ||
}); | ||
it('should fail when requiring a valid extension (js) but invalid file path', () => { | ||
expect(() => requireFile('./__fixtures__/invalid.js')).toThrowError( | ||
`Cannot find module './__fixtures__/invalid.js' from 'file.ts'`, | ||
); | ||
}); | ||
it('should fail when requiring a valid extension (json) but invalid file path', () => { | ||
expect(() => requireFile('./__fixtures__/npmignore.json')).toThrowError( | ||
`Cannot find module './__fixtures__/npmignore.json' from 'file.ts'`, | ||
); | ||
}); | ||
it('should require file with no extension using js', async () => { | ||
const config = await requireFile( | ||
path.join(__dirname, '__fixtures__', 'file'), | ||
); | ||
await expect(config.example).toEqual(true); | ||
}); | ||
it('should require file with no extension using json', async () => { | ||
const file = await requireFile( | ||
path.join(__dirname, '__fixtures__', 'package'), | ||
); | ||
expect(file.name).toEqual('example'); | ||
}); | ||
it('should fail when requiring a file with no extension', async () => { | ||
expect(() => | ||
requireFile(path.join(__dirname, '__fixtures__', 'packages')), | ||
).toThrowError( | ||
`Cannot find module '${__dirname}/__fixtures__/packages.json' from 'file.ts'`, | ||
); | ||
}); | ||
}); | ||
|
||
describe('resolveFile', () => { | ||
it('should resolve when path has extension', () => { | ||
const resolvedPath = resolveFile( | ||
path.join(__dirname, './__fixtures__/package.json'), | ||
); | ||
expect(resolvedPath).toEqual( | ||
require.resolve(path.join(__dirname, '__fixtures__/package.json')), | ||
); | ||
}); | ||
|
||
it('should resolve when path has extension', () => { | ||
const resolvedPath = resolveFile( | ||
path.join(__dirname, '__fixtures__', 'package'), | ||
); | ||
expect(resolvedPath).toEqual( | ||
require.resolve(path.join(__dirname, '__fixtures__', 'package')), | ||
); | ||
}); | ||
|
||
it('should resolve when path has extension', () => { | ||
const resolvedPath = resolveFile( | ||
path.join(__dirname, '__fixtures__', 'package'), | ||
); | ||
expect(resolvedPath).toEqual( | ||
require.resolve(path.join(__dirname, '__fixtures__', 'package')), | ||
); | ||
}); | ||
|
||
it('should resolve when path has extension but path is not found', () => { | ||
expect(() => resolveFile('./__fixtures__/nonexistant.js')).toThrowError( | ||
`Cannot find module './__fixtures__/nonexistant.js' from 'file.ts'`, | ||
); | ||
}); | ||
|
||
it('should resolve when path has no extension', () => { | ||
const resolvedPath = resolveFile( | ||
path.join(__dirname, '__fixtures__', 'file'), | ||
); | ||
expect(resolvedPath).toEqual( | ||
require.resolve(path.join(__dirname, '__fixtures__', 'file')), | ||
); | ||
}); | ||
}); |
110 changes: 0 additions & 110 deletions
110
packages/graphql-language-service-utils/src/file.spec.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,5 +8,6 @@ | |
{ | ||
"path": "../graphql-language-service-types" | ||
} | ||
] | ||
], | ||
"exclude": ["**/*-test.*","**/*.spec.*"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters