pulsar/spec/package-transpilation-registry-spec.js

238 lines
7.5 KiB
JavaScript
Raw Normal View History

2016-10-30 20:21:22 +03:00
/** @babel */
import path from 'path'
import PackageTranspilationRegistry from '../src/package-transpilation-registry'
2016-11-01 18:12:33 +03:00
const originalCompiler = {
2016-10-30 20:21:22 +03:00
getCachePath: (sourceCode, filePath) => {
2019-02-22 10:55:17 +03:00
return 'orig-cache-path'
2016-10-30 20:21:22 +03:00
},
compile: (sourceCode, filePath) => {
2019-02-22 10:55:17 +03:00
return sourceCode + '-original-compiler'
2016-10-30 20:21:22 +03:00
},
shouldCompile: (sourceCode, filePath) => {
return path.extname(filePath) === '.js'
}
}
2019-02-22 10:55:17 +03:00
describe('PackageTranspilationRegistry', () => {
2016-10-30 20:21:22 +03:00
let registry
let wrappedCompiler
beforeEach(() => {
registry = new PackageTranspilationRegistry()
wrappedCompiler = registry.wrapTranspiler(originalCompiler)
})
it('falls through to the original compiler by default', () => {
spyOn(originalCompiler, 'getCachePath')
spyOn(originalCompiler, 'compile')
spyOn(originalCompiler, 'shouldCompile')
wrappedCompiler.getCachePath('source', '/path/to/file.js')
wrappedCompiler.compile('source', '/path/to/filejs')
wrappedCompiler.shouldCompile('source', '/path/to/file.js')
expect(originalCompiler.getCachePath).toHaveBeenCalled()
expect(originalCompiler.compile).toHaveBeenCalled()
expect(originalCompiler.shouldCompile).toHaveBeenCalled()
})
describe('when a file is contained in a path that has custom transpilation', () => {
const hitPath = path.join('/path/to/lib/file.js')
const hitPathCoffee = path.join('/path/to/file2.coffee')
const missPath = path.join('/path/other/file3.js')
2019-02-22 10:55:17 +03:00
const hitPathMissSubdir = path.join('/path/to/file4.js')
const hitPathMissExt = path.join('/path/to/file5.ts')
const nodeModulesFolder = path.join('/path/to/lib/node_modules/file6.js')
const hitNonStandardExt = path.join('/path/to/file7.omgwhatisthis')
2016-11-01 18:12:33 +03:00
2019-02-22 10:55:17 +03:00
const jsSpec = {
glob: 'lib/**/*.js',
transpiler: './transpiler-js',
options: { type: 'js' }
}
const coffeeSpec = {
glob: '*.coffee',
transpiler: './transpiler-coffee',
options: { type: 'coffee' }
}
const omgSpec = {
glob: '*.omgwhatisthis',
transpiler: './transpiler-omg',
options: { type: 'omg' }
}
2016-11-01 18:12:33 +03:00
2019-02-22 10:55:17 +03:00
const expectedMeta = {
name: 'my-package',
path: path.join('/path/to'),
meta: { some: 'metadata' }
}
2016-11-01 20:54:26 +03:00
2016-11-01 18:12:33 +03:00
const jsTranspiler = {
2016-10-30 20:21:22 +03:00
transpile: (sourceCode, filePath, options) => {
2019-02-22 10:55:17 +03:00
return { code: sourceCode + '-transpiler-js' }
2016-10-30 20:21:22 +03:00
},
getCacheKeyData: (sourceCode, filePath, options) => {
return 'js-transpiler-cache-data'
}
}
2016-11-01 18:12:33 +03:00
const coffeeTranspiler = {
2016-10-30 20:21:22 +03:00
transpile: (sourceCode, filePath, options) => {
2019-02-22 10:55:17 +03:00
return { code: sourceCode + '-transpiler-coffee' }
2016-10-30 20:21:22 +03:00
},
getCacheKeyData: (sourceCode, filePath, options) => {
return 'coffee-transpiler-cache-data'
}
}
2016-11-01 18:12:33 +03:00
const omgTranspiler = {
transpile: (sourceCode, filePath, options) => {
2019-02-22 10:55:17 +03:00
return { code: sourceCode + '-transpiler-omg' }
},
getCacheKeyData: (sourceCode, filePath, options) => {
return 'omg-transpiler-cache-data'
}
}
2016-10-30 20:21:22 +03:00
beforeEach(() => {
2019-02-22 10:55:17 +03:00
jsSpec._transpilerSource = 'js-transpiler-source'
coffeeSpec._transpilerSource = 'coffee-transpiler-source'
omgTranspiler._transpilerSource = 'omg-transpiler-source'
2016-10-30 20:21:22 +03:00
2019-02-22 10:55:17 +03:00
spyOn(registry, 'getTranspiler').andCallFake(spec => {
2016-10-30 20:21:22 +03:00
if (spec.transpiler === './transpiler-js') return jsTranspiler
if (spec.transpiler === './transpiler-coffee') return coffeeTranspiler
if (spec.transpiler === './transpiler-omg') return omgTranspiler
2016-10-30 20:21:22 +03:00
throw new Error('bad transpiler path ' + spec.transpiler)
})
2019-02-22 10:55:17 +03:00
registry.addTranspilerConfigForPath(
path.join('/path/to'),
'my-package',
{ some: 'metadata' },
[jsSpec, coffeeSpec, omgSpec]
)
2016-10-30 20:21:22 +03:00
})
it('always returns true from shouldCompile for a file in that dir that match a glob', () => {
spyOn(originalCompiler, 'shouldCompile').andReturn(false)
expect(wrappedCompiler.shouldCompile('source', hitPath)).toBe(true)
expect(wrappedCompiler.shouldCompile('source', hitPathCoffee)).toBe(true)
2019-02-22 10:55:17 +03:00
expect(wrappedCompiler.shouldCompile('source', hitNonStandardExt)).toBe(
true
)
expect(wrappedCompiler.shouldCompile('source', hitPathMissExt)).toBe(
false
)
expect(wrappedCompiler.shouldCompile('source', hitPathMissSubdir)).toBe(
false
)
2016-10-30 20:21:22 +03:00
expect(wrappedCompiler.shouldCompile('source', missPath)).toBe(false)
2019-02-22 10:55:17 +03:00
expect(wrappedCompiler.shouldCompile('source', nodeModulesFolder)).toBe(
false
)
2016-10-30 20:21:22 +03:00
})
it('calls getCacheKeyData on the transpiler to get additional cache key data', () => {
2019-02-22 10:55:17 +03:00
spyOn(registry, 'getTranspilerPath').andReturn('./transpiler-js')
2016-10-30 20:21:22 +03:00
spyOn(jsTranspiler, 'getCacheKeyData').andCallThrough()
wrappedCompiler.getCachePath('source', missPath, jsSpec)
2019-02-22 10:55:17 +03:00
expect(jsTranspiler.getCacheKeyData).not.toHaveBeenCalledWith(
'source',
missPath,
jsSpec.options,
expectedMeta
)
2016-10-30 20:21:22 +03:00
wrappedCompiler.getCachePath('source', hitPath, jsSpec)
2019-02-22 10:55:17 +03:00
expect(jsTranspiler.getCacheKeyData).toHaveBeenCalledWith(
'source',
hitPath,
jsSpec.options,
expectedMeta
)
2016-10-30 20:21:22 +03:00
})
it('compiles files matching a glob with the associated transpiler, and the old one otherwise', () => {
2019-02-22 10:55:17 +03:00
spyOn(jsTranspiler, 'transpile').andCallThrough()
spyOn(coffeeTranspiler, 'transpile').andCallThrough()
spyOn(omgTranspiler, 'transpile').andCallThrough()
expect(wrappedCompiler.compile('source', hitPath)).toEqual(
'source-transpiler-js'
)
expect(jsTranspiler.transpile).toHaveBeenCalledWith(
'source',
hitPath,
jsSpec.options,
expectedMeta
)
expect(wrappedCompiler.compile('source', hitPathCoffee)).toEqual(
'source-transpiler-coffee'
)
expect(coffeeTranspiler.transpile).toHaveBeenCalledWith(
'source',
hitPathCoffee,
coffeeSpec.options,
expectedMeta
)
expect(wrappedCompiler.compile('source', hitNonStandardExt)).toEqual(
'source-transpiler-omg'
)
expect(omgTranspiler.transpile).toHaveBeenCalledWith(
'source',
hitNonStandardExt,
omgSpec.options,
expectedMeta
)
expect(wrappedCompiler.compile('source', missPath)).toEqual(
'source-original-compiler'
)
expect(wrappedCompiler.compile('source', hitPathMissExt)).toEqual(
'source-original-compiler'
)
expect(wrappedCompiler.compile('source', hitPathMissSubdir)).toEqual(
'source-original-compiler'
)
expect(wrappedCompiler.compile('source', nodeModulesFolder)).toEqual(
'source-original-compiler'
)
2016-10-30 20:21:22 +03:00
})
2019-02-22 10:55:17 +03:00
describe('when the packages root path contains node_modules', () => {
beforeEach(() => {
2019-02-22 10:55:17 +03:00
registry.addTranspilerConfigForPath(
path.join('/path/with/node_modules/in/root'),
'my-other-package',
{ some: 'metadata' },
[jsSpec]
)
})
it('returns appropriate values from shouldCompile', () => {
spyOn(originalCompiler, 'shouldCompile').andReturn(false)
2019-02-22 10:55:17 +03:00
expect(
wrappedCompiler.shouldCompile(
'source',
'/path/with/node_modules/in/root/lib/test.js'
)
).toBe(true)
expect(
wrappedCompiler.shouldCompile(
'source',
'/path/with/node_modules/in/root/lib/node_modules/test.js'
)
).toBe(false)
})
})
2016-10-30 20:21:22 +03:00
})
})