-
Notifications
You must be signed in to change notification settings - Fork 674
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CoffeeScript support (close #1556) #2651
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ea77482
CoffeeScript compiler first pass
GeoffreyBooth aef84d6
Tests for CoffeeScript compiler
GeoffreyBooth e610d60
Don’t use CoffeeScript’s internal Babel to compile, use the same Babe…
GeoffreyBooth c2fd54c
CoffeeScript functional tests that mirror TypeScript’s
GeoffreyBooth e740d70
Don’t crash on variable declaration lines like `var foo;` (this appar…
GeoffreyBooth e8186da
Don’t use CoffeeScript safety wrapper, so that the AST nodes we’re lo…
GeoffreyBooth dd45f9c
CoffeeScript parser smoke tests; allow returns of `test` calls
GeoffreyBooth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import CoffeeScript from 'coffeescript'; | ||
import loadBabelLibs from '../../../load-babel-libs'; | ||
import ESNextTestFileCompiler from '../es-next/compiler.js'; | ||
|
||
const FIXTURE_RE = /(^|;|\s+)fixture\s*(\.|\(|'|")/; | ||
const TEST_RE = /(^|;|\s+)test\s*/; | ||
|
||
export default class CoffeeScriptTestFileCompiler extends ESNextTestFileCompiler { | ||
_hasTests (code) { | ||
return FIXTURE_RE.test(code) && TEST_RE.test(code); | ||
} | ||
|
||
_compileCode (code, filename) { | ||
if (this.cache[filename]) | ||
return this.cache[filename]; | ||
|
||
var transpiled = CoffeeScript.compile(code, { | ||
filename, | ||
bare: true, | ||
sourceMap: true, | ||
inlineMap: true, | ||
header: false | ||
}); | ||
|
||
var { babel } = loadBabelLibs(); | ||
var babelOptions = ESNextTestFileCompiler.getBabelOptions(filename, code); | ||
var compiled = babel.transform(transpiled.js, babelOptions); | ||
|
||
this.cache[filename] = compiled.code; | ||
|
||
return compiled.code; | ||
} | ||
|
||
_getRequireCompilers () { | ||
return { '.coffee': (code, filename) => this._compileCode(code, filename) }; | ||
} | ||
|
||
getSupportedExtension () { | ||
return '.coffee'; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/compiler/test-file/formats/coffeescript/get-test-list.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,29 @@ | ||
import CoffeeScript from 'coffeescript'; | ||
import { transform } from 'babel-core'; | ||
import ESNextTestFileCompiler from '../es-next/compiler.js'; | ||
import { EsNextTestFileParser } from '../es-next/get-test-list'; | ||
|
||
export class CoffeeScriptTestFileParser extends EsNextTestFileParser { | ||
parse (code) { | ||
const babelOptions = ESNextTestFileCompiler.getBabelOptions(null, code); | ||
|
||
delete babelOptions.filename; | ||
babelOptions.ast = true; | ||
|
||
code = CoffeeScript.compile(code, { | ||
bare: true, | ||
sourceMap: false, | ||
inlineMap: false, | ||
header: false | ||
}); | ||
|
||
const ast = transform(code, babelOptions).ast; | ||
|
||
return this.analyze(ast.program.body); | ||
} | ||
} | ||
|
||
const parser = new CoffeeScriptTestFileParser(); | ||
|
||
export const getCoffeeScriptTestList = parser.getTestList.bind(parser); | ||
export const getCoffeeScriptTestListFromCode = parser.getTestListFromCode.bind(parser); |
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
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
var expect = require('chai').expect; | ||
|
||
describe('[CoffeeScript] Smoke tests', function () { | ||
it('Should run non-trivial tests', function () { | ||
return runTests('./testcafe-fixtures/non-trivial-test.coffee', null, { selectorTimeout: 5000 }); | ||
}); | ||
|
||
it('Should produce correct callsites on error', function () { | ||
return runTests('./testcafe-fixtures/callsite-test.coffee', null, { shouldFail: true }) | ||
.catch(function (errs) { | ||
expect(errs[0]).contains('The specified selector does not match any element in the DOM tree.'); | ||
expect(errs[0]).contains('> 5 |doSmthg = (selector, t) -> await t.click selector'); | ||
}); | ||
}); | ||
}); |
8 changes: 8 additions & 0 deletions
8
test/functional/fixtures/api/coffeescript/smoke/testcafe-fixtures/callsite-test.coffee
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,8 @@ | ||
import 'testcafe' | ||
|
||
fixture 'CoffeeScript callsites' | ||
|
||
doSmthg = (selector, t) -> await t.click selector | ||
|
||
test 'Test', (t) => | ||
await doSmthg '#heyheyhey', t |
59 changes: 59 additions & 0 deletions
59
test/functional/fixtures/api/coffeescript/smoke/testcafe-fixtures/non-trivial-test.coffee
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,59 @@ | ||
import { Selector, Role, t } from 'testcafe' | ||
|
||
iframeElement = Selector '#element-in-iframe' | ||
pageElement = Selector '#element-on-page' | ||
showAlertBtn = Selector '#show-alert' | ||
|
||
initConfiguration = -> | ||
await t | ||
.setNativeDialogHandler => on | ||
.click showAlertBtn | ||
|
||
history = await t.getNativeDialogHistory() | ||
|
||
await t | ||
.expect(history[0].text).eql 'Hey!' | ||
.switchToIframe '#iframe' | ||
.expect(iframeElement.exists).ok() | ||
.setTestSpeed 0.95 | ||
.setPageLoadTimeout 95 | ||
|
||
t.ctx['someVal'] = 'ctxVal' | ||
t.fixtureCtx['someVal'] = 'fixtureCtxVal' | ||
|
||
role1 = Role 'http://localhost:3000/fixtures/api/es-next/roles/pages/index.html', => | ||
await t | ||
.setNativeDialogHandler => on | ||
|
||
await t | ||
.expect(pageElement.exists).ok() | ||
.expect(t.ctx['someVal']).notOk() | ||
.expect(t.fixtureCtx['someVal']).notOk() | ||
|
||
await t.click showAlertBtn | ||
|
||
role2 = Role 'http://localhost:3000/fixtures/api/es-next/roles/pages/index.html', => | ||
|
||
fixture 'CoffeeScript smoke tests' | ||
.page 'http://localhost:3000/fixtures/api/es-next/roles/pages/index.html' | ||
|
||
test 'Clear configuration', => | ||
await initConfiguration() | ||
await t.useRole role1 | ||
|
||
test 'Restore configuration', => | ||
await initConfiguration() | ||
|
||
await t | ||
.useRole role2 | ||
.expect(iframeElement.exists).ok() | ||
.expect(t.ctx['someVal']).eql 'ctxVal' | ||
.expect(t.fixtureCtx['someVal']).eql 'fixtureCtxVal' | ||
|
||
await t | ||
.switchToMainWindow() | ||
.click showAlertBtn | ||
|
||
history = await t.getNativeDialogHistory() | ||
|
||
await t.expect(history[0].text).eql 'Hey!' |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export default -> | ||
'Hey from dep1' |
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,4 @@ | ||
import dep1Fn from './dep1' | ||
|
||
export default -> | ||
"#{await dep1Fn()} and dep2" |
21 changes: 21 additions & 0 deletions
21
test/server/data/test-suites/coffeescript-basic/testfile1.coffee
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,21 @@ | ||
import 'testcafe' | ||
import dep1Fn from './dep1' | ||
|
||
fixture 'Fixture1' | ||
|
||
test 'Fixture1Test1', => | ||
res = await dep1Fn() | ||
"F1T1: #{res}" | ||
|
||
test2Name = 'Fixture1Test2' | ||
|
||
test test2Name, => | ||
'F1T2' | ||
|
||
fixture "Fixture#{1 + 1}" | ||
.page 'http://example.org' | ||
.beforeEach => 'yo' | ||
.afterEach => 'yo' | ||
|
||
test 'Fixture2Test1', => | ||
'F2T1' |
13 changes: 13 additions & 0 deletions
13
test/server/data/test-suites/coffeescript-basic/testfile2.coffee
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,13 @@ | ||
import 'testcafe' | ||
import dep2Fn from './dep2' | ||
|
||
fixture 'Fixture3' | ||
.page 'https://example.com' | ||
.afterEach => 'yo' | ||
.beforeEach => 'yo' | ||
|
||
fixture3Name = 'Fixture3Test1' | ||
|
||
test fixture3Name, => | ||
res = await dep2Fn() | ||
"F3T1: #{res}" |
15 changes: 15 additions & 0 deletions
15
test/server/data/test-suites/coffeescript-parser-smoke/testfile1.coffee
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,15 @@ | ||
getPageUrl = (index) -> | ||
"http://page/#{index}" | ||
|
||
fixture('fixture 1').page getFixtureName(1) | ||
|
||
doSmthg = (selector, t) -> | ||
await t.click selector | ||
|
||
test 'test 1', (t) => | ||
await doSmthg '#my-el', t | ||
|
||
((fixtureName, testName) -> | ||
fixture(fixtureName).page 'http://myPage' | ||
test testName, (t) => | ||
) 'fixture 2', 'test 2' |
5 changes: 5 additions & 0 deletions
5
test/server/data/test-suites/coffeescript-parser-smoke/testfile2.coffee
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,5 @@ | ||
fixture('fixture 1').page 'https://page' | ||
|
||
test.before((t) => | ||
await t.wait 1 | ||
) 'test 1' |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's okay to override the
canCompile
method if you want to support files with different extensions, as you stated in your comment in the nodejs repository. Our Legacy API compiler does it: testcafe-legacy-api/blob/master/src/compiler/index.js#L157