-
Notifications
You must be signed in to change notification settings - Fork 12k
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
fix(tests): add global scripts in karma plugin #3543
Merged
filipesilva
merged 2 commits into
angular:master
from
filipesilva:fix-scripts-karma-plugin
Dec 22, 2016
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { writeMultipleFiles } from '../../utils/fs'; | ||
import { ng } from '../../utils/process'; | ||
import { updateJsonFile } from '../../utils/project'; | ||
import { expectToFail } from '../../utils/utils'; | ||
import { stripIndent } from 'common-tags'; | ||
|
||
// Make sure asset files are served | ||
export default function () { | ||
return Promise.resolve() | ||
.then(() => writeMultipleFiles({ | ||
'src/assets/file.txt': 'assets-folder-content', | ||
'src/file.txt': 'file-content', | ||
// Not using `async()` in tests as it seemed to swallow `fetch()` errors | ||
'src/app/app.component.spec.ts': stripIndent` | ||
describe('Test Runner', () => { | ||
const fetch = global['fetch']; | ||
it('should serve files in assets folder', (done) => { | ||
fetch('/assets/file.txt') | ||
.then(response => response.text()) | ||
.then(fileText => { | ||
expect(fileText).toMatch('assets-folder-content'); | ||
done(); | ||
}); | ||
}); | ||
it('should serve files explicitly added to assets array', (done) => { | ||
fetch('/file.txt') | ||
.then(response => response.text()) | ||
.then(fileText => { | ||
expect(fileText).toMatch('file-content'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
` | ||
})) | ||
// Test failure condition (no assets in angular-cli.json) | ||
.then(() => updateJsonFile('angular-cli.json', configJson => { | ||
const app = configJson['apps'][0]; | ||
app['assets'] = []; | ||
})) | ||
.then(() => expectToFail(() => ng('test', '--single-run'), | ||
'Should fail because the assets to serve were not in the angular-cli config')) | ||
// Test passing condition (assets are included) | ||
.then(() => updateJsonFile('angular-cli.json', configJson => { | ||
const app = configJson['apps'][0]; | ||
app['assets'] = ['assets', 'file.txt']; | ||
})) | ||
.then(() => ng('test', '--single-run')); | ||
} |
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,71 @@ | ||
import { writeMultipleFiles } from '../../utils/fs'; | ||
import { ng } from '../../utils/process'; | ||
import { updateJsonFile } from '../../utils/project'; | ||
import { expectToFail } from '../../utils/utils'; | ||
import { stripIndent } from 'common-tags'; | ||
|
||
export default function () { | ||
return Promise.resolve() | ||
.then(() => ng('test', '--watch=false')) | ||
// prepare global scripts test files | ||
.then(() => writeMultipleFiles({ | ||
'src/string-script.js': `stringScriptGlobal = 'string-scripts.js';`, | ||
'src/input-script.js': `inputScriptGlobal = 'input-scripts.js';`, | ||
'src/typings.d.ts': stripIndent` | ||
declare var stringScriptGlobal: any; | ||
declare var inputScriptGlobal: any; | ||
`, | ||
'src/app/app.component.ts': stripIndent` | ||
import { Component } from '@angular/core'; | ||
|
||
@Component({ selector: 'app-root', template: '' }) | ||
export class AppComponent { | ||
stringScriptGlobalProp = stringScriptGlobal; | ||
inputScriptGlobalProp = inputScriptGlobal; | ||
} | ||
`, | ||
'src/app/app.component.spec.ts': stripIndent` | ||
import { TestBed, async } from '@angular/core/testing'; | ||
import { AppComponent } from './app.component'; | ||
|
||
describe('AppComponent', () => { | ||
beforeEach(() => { | ||
TestBed.configureTestingModule({ declarations: [ AppComponent ] }); | ||
TestBed.compileComponents(); | ||
}); | ||
|
||
it('should have access to string-script.js', async(() => { | ||
let app = TestBed.createComponent(AppComponent).debugElement.componentInstance; | ||
expect(app.stringScriptGlobalProp).toEqual('string-scripts.js'); | ||
})); | ||
|
||
it('should have access to input-script.js', async(() => { | ||
let app = TestBed.createComponent(AppComponent).debugElement.componentInstance; | ||
expect(app.inputScriptGlobalProp).toEqual('input-scripts.js'); | ||
})); | ||
}); | ||
|
||
describe('Spec', () => { | ||
it('should have access to string-script.js', async(() => { | ||
expect(stringScriptGlobal).toBe('string-scripts.js'); | ||
})); | ||
|
||
it('should have access to input-script.js', async(() => { | ||
expect(inputScriptGlobal).toBe('input-scripts.js'); | ||
})); | ||
}); | ||
` | ||
})) | ||
// should fail because the global scripts were not added to scripts array | ||
.then(() => expectToFail(() => ng('test', '--single-run'))) | ||
.then(() => updateJsonFile('angular-cli.json', configJson => { | ||
const app = configJson['apps'][0]; | ||
app['scripts'] = [ | ||
'string-script.js', | ||
{ input: 'input-script.js' } | ||
]; | ||
})) | ||
// should pass now | ||
.then(() => ng('test', '--single-run')); | ||
} | ||
|
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 |
---|---|---|
@@ -1,65 +1,7 @@ | ||
import { writeMultipleFiles } from '../../utils/fs'; | ||
import { ng } from '../../utils/process'; | ||
import { updateJsonFile } from '../../utils/project'; | ||
import { expectToFail } from '../../utils/utils'; | ||
import { stripIndent } from 'common-tags'; | ||
|
||
|
||
export default function () { | ||
// Each test function returns PromiseLike<Something_Different>, | ||
// which would throw normally | ||
// but resolve() normalizes this to <any> from the start | ||
return Promise.resolve() | ||
.then(() => testWatchFalseAndSingleRun()) | ||
.then(() => testAssetsAreServed()); | ||
} | ||
|
||
// Make sure both --watch=false and --single-run work | ||
function testWatchFalseAndSingleRun() { | ||
// make sure both --watch=false and --single-run work | ||
return ng('test', '--single-run') | ||
.then(() => ng('test', '--watch=false')); | ||
} | ||
|
||
// Make sure asset files are served | ||
function testAssetsAreServed() { | ||
return Promise.resolve() | ||
.then(() => writeMultipleFiles({ | ||
'src/assets/file.txt': 'assets-folder-content', | ||
'src/file.txt': 'file-content', | ||
// Not using `async()` in tests as it seemed to swallow `fetch()` errors | ||
'src/app/app.component.spec.ts': stripIndent` | ||
describe('Test Runner', () => { | ||
const fetch = global['fetch']; | ||
it('should serve files in assets folder', (done) => { | ||
fetch('/assets/file.txt') | ||
.then(response => response.text()) | ||
.then(fileText => { | ||
expect(fileText).toMatch('assets-folder-content'); | ||
done(); | ||
}); | ||
}); | ||
it('should serve files explicitly added to assets array', (done) => { | ||
fetch('/file.txt') | ||
.then(response => response.text()) | ||
.then(fileText => { | ||
expect(fileText).toMatch('file-content'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
` | ||
})) | ||
// Test failure condition (no assets in angular-cli.json) | ||
.then(() => updateJsonFile('angular-cli.json', configJson => { | ||
const app = configJson['apps'][0]; | ||
app['assets'] = []; | ||
})) | ||
.then(() => expectToFail(() => ng('test', '--single-run'), | ||
'Should fail because the assets to serve were not in the angular-cli config')) | ||
// Test passing condition (assets are included) | ||
.then(() => updateJsonFile('angular-cli.json', configJson => { | ||
const app = configJson['apps'][0]; | ||
app['assets'] = ['assets', 'file.txt']; | ||
})) | ||
.then(() => ng('test', '--single-run')); | ||
} |
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.
config.files.unshift(...globalScriptsPatterns)
is simpler.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.
We can't do that in the karma plugin since it's not preprocessed and node4 does not support the spread operator (without a flag).
I hope to tackle that in #3605 by making the plugin be in TypeScript.
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.
Sounds good.