-
-
Notifications
You must be signed in to change notification settings - Fork 276
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
Add End 2 End tests #477
Merged
Merged
Add End 2 End tests #477
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3db0b6e
Basic setup to run e2e tests on CI
3b4909a
Run e2e test against github.com/OctoLinker/e2e/
403143c
Scrape e2e repo for urls
16e99ea
Run e2e tests
0c8dd8d
Get fixtures from e2e repo
d478118
Remove not longer needed package node-fetch
b5afde3
Add e2e fixtures
c700f67
Generate fixtures resource file
5fbc90b
Add E2E readme file
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
dist/ | ||
packages/**/node_modules | ||
e2e/fixtures/ |
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 |
---|---|---|
|
@@ -4,3 +4,4 @@ lerna-debug.log | |
|
||
dist/ | ||
out/ | ||
e2e/fixtures.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
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 @@ | ||
module.exports = { | ||
globals: { | ||
page: true | ||
} | ||
} |
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,68 @@ | ||
# OctoLinker E2E Tests | ||
|
||
## What is E2E Testing? | ||
|
||
E2E stand for End to End testing and the following lines explains it the best which I have copied from [Protractor](https://docs.angularjs.org/guide/e2e-testing); | ||
|
||
> As applications grow in size and complexity, it becomes unrealistic to rely on manual testing to verify the correctness of new features, catch bugs and notice regressions. Unit tests are the first line of defense for catching bugs, but sometimes issues come up with integration between components which can't be captured in a unit test. End-to-end tests are made to find these problems. | ||
|
||
### How we do E2E tests | ||
|
||
We're using [Puppeteer](https://github.com/GoogleChrome/puppeteer) which allows us to spin up a Chrome instance which then loads the OctoLinker extension. Once Chrome has started we can navigate to any website which is github.com in our case. In this directory we also have a folder called [fixtures](https://github.com/OctoLinker/OctoLinker/tree/master/e2e/fixtures) which contains all our dummy files which we use for testing. | ||
|
||
If you open one of those files, you will see a special annotation which we use to specify that the next line should resolve to the given target. | ||
|
||
```js | ||
// When the target is an external url | ||
// @OctoLinkerResolve(https://nodejs.org/api/fs.html) | ||
require('fs') | ||
|
||
// When the target is a relative file | ||
// @OctoLinkerResolve(<root>/path/to/foo.js) | ||
require('./foo.js') | ||
``` | ||
|
||
The annotation is language agnostic so you can also use it for ruby or any other language. | ||
|
||
```ruby | ||
# @OctoLinkerResolve(https://rubygems.org/gems/rake) | ||
require 'rake' | ||
``` | ||
|
||
It's also working within json files. | ||
|
||
```js | ||
{ | ||
"dependencies": { | ||
"//": "@OctoLinkerResolve(https://github.com/lodash/lodash)", | ||
"lodash": "^1.2.3" | ||
} | ||
} | ||
``` | ||
|
||
## How do we know | ||
|
||
Before we invoke [jest](https://github.com/facebook/jest), we scan the fixtures folder for those annotation and write a file named `fixtures.json` on the disk. This json file then gets loaded by our actual test file (https://github.com/OctoLinker/OctoLinker/blob/master/e2e/automated.test.js) to scaffold those tests on-the-fly. | ||
|
||
|
||
```js | ||
[ | ||
{ | ||
"url": "https://github.com/OctoLinker/OctoLinker/blob/master/e2e/fixtures/javascript/nodejs/gentle-resonance-3436.js", | ||
"content": "require('./proud-tooth-7361');", | ||
"targetUrl": "/javascript/nodejs/proud-tooth-7361.js", | ||
"lineNumber": 2 | ||
}, | ||
{ | ||
"url": "https://github.com/OctoLinker/OctoLinker/blob/master/e2e/fixtures/javascript/npm/package.json", | ||
"content": "\"underscore\": \"1.2.3\"", | ||
"targetUrl": "https://github.com/jashkenas/underscore", | ||
"lineNumber": 8 | ||
}, | ||
... | ||
] | ||
``` | ||
|
||
## Contribution | ||
|
||
We made this process super simple. Just create files within `e2e/fixtures` and make sure the annotation target is set correctly, that's all. If you have a minute or two we would really appreciate a PullReqeust from you. |
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,25 @@ | ||
const fixtures = require('./fixtures.json'); // eslint-disable-line import/no-unresolved | ||
|
||
async function executeTest(url, lineNumber, targetUrl) { | ||
const selector = `#LC${lineNumber} .octolinker-link`; | ||
|
||
await page.goto(url); | ||
|
||
await page.waitForSelector(selector); | ||
|
||
await Promise.all([ | ||
page.waitForNavigation(), | ||
// page.click(selector), for some reason page.click is not working | ||
page.$eval(selector, el => el.click()), | ||
]); | ||
|
||
await expect(page.url()).toEqual(expect.stringMatching(targetUrl)); | ||
} | ||
|
||
describe('End to End tests', () => { | ||
fixtures.forEach(({ url, content, lineNumber, targetUrl }) => { | ||
it(`resolves ${content} to ${targetUrl}`, async () => { | ||
await executeTest(url, lineNumber, targetUrl); | ||
}); | ||
}); | ||
}); |
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 @@ | ||
// @OctoLinkerResolve(<root>/javascript/nodejs/proud-tooth-7361.js) | ||
require('./proud-tooth-7361'); |
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,11 @@ | ||
// @OctoLinkerResolve(<root>/javascript/nodejs/gentle-resonance-3436.js) | ||
require('./gentle-resonance-3436.js'); | ||
|
||
// @OctoLinkerResolve(<root>/javascript/nodejs/proud-tooth-7361.js) | ||
require('./proud-tooth-7361'); | ||
|
||
// @OctoLinkerResolve(https://nodejs.org/api/fs.html) | ||
require('fs'); | ||
|
||
// @OctoLinkerResolve(https://github.com/lodash/lodash) | ||
require('lodash'); |
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 @@ | ||
// @OctoLinkerResolve(<root>/javascript/nodejs/index.js) | ||
require('.'); |
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,10 @@ | ||
{ | ||
"dependencies": { | ||
"//": "@OctoLinkerResolve(https://github.com/lodash/lodash)", | ||
"lodash": "^1.2.3" | ||
}, | ||
"devDependencies": { | ||
"//": "@OctoLinkerResolve(https://github.com/jashkenas/underscore)", | ||
"underscore": "1.2.3" | ||
} | ||
} |
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,76 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const util = require('util'); | ||
const recursive = require('recursive-readdir'); | ||
const async = require('async'); | ||
|
||
// This will generate a json file which is consumed by automated.test.js in the following format. | ||
// Checkout out https://github.com/OctoLinker/OctoLinker/blob/master/e2e/README.md for details. | ||
// | ||
// [ | ||
// { | ||
// "url": "https://github.com/OctoLinker/OctoLinker/blob/master/e2e/fixtures/javascript/nodejs/gentle-resonance-3436.js", | ||
// "content": "require('./proud-tooth-7361');", | ||
// "targetUrl": "/javascript/nodejs/proud-tooth-7361.js", | ||
// "lineNumber": 2 | ||
// }, | ||
// ... | ||
// ] | ||
|
||
const repoSlug = | ||
process.env.TRAVIS_PULL_REQUEST_SLUG || 'OctoLinker/OctoLinker'; | ||
const branch = process.env.TRAVIS_PULL_REQUEST_BRANCH || 'master'; | ||
const fixturesRoot = `https://github.com/${repoSlug}/blob/${branch}/e2e`; | ||
|
||
async function readContent(files) { | ||
const readFile = util.promisify(fs.readFile); | ||
const mapLimit = util.promisify(async.mapLimit); | ||
|
||
return mapLimit(files, 10, async file => { | ||
const content = await readFile(file, 'utf8'); | ||
|
||
return { | ||
file, | ||
content, | ||
}; | ||
}); | ||
} | ||
|
||
function findTests(contents) { | ||
return contents.reduce((memo, { file, content }) => { | ||
const lines = content.split('\n'); | ||
|
||
lines.forEach((line, index) => { | ||
if (line.includes('@OctoLinkerResolve')) { | ||
const lineNumber = index + 2; | ||
const targetUrl = line | ||
.match(/@OctoLinkerResolve\((.*?)\)/)[1] | ||
.replace('<root>', ''); | ||
|
||
const filePath = file.replace(__dirname, ''); | ||
|
||
memo.push({ | ||
url: `${fixturesRoot}${filePath}`, | ||
content: lines[index + 1].trim(), | ||
targetUrl, | ||
lineNumber, | ||
}); | ||
} | ||
}); | ||
|
||
return memo; | ||
}, []); | ||
} | ||
|
||
(async function init() { | ||
const files = await recursive(path.join(__dirname, 'fixtures')); | ||
const contents = await readContent(files); | ||
const out = findTests(contents); | ||
|
||
console.log(out); // eslint-disable-line | ||
|
||
fs.writeFileSync( | ||
path.join(__dirname, 'fixtures.json'), | ||
JSON.stringify(out, null, ' '), | ||
); | ||
})(); |
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 @@ | ||
module.exports = { | ||
preset: 'jest-puppeteer', | ||
testMatch: ['<rootDir>/**/?(*.)test?(s).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,15 @@ | ||
const path = require('path'); | ||
|
||
const extPath = path.join(__dirname, './dist'); | ||
|
||
module.exports = { | ||
launch: { | ||
dumpio: true, | ||
headless: false, | ||
args: [ | ||
'--no-sandbox', | ||
`--disable-extensions-except=${extPath}`, | ||
`--load-extension=${extPath}`, | ||
], | ||
}, | ||
}; |
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,7 @@ | ||
module.exports = { | ||
testPathIgnorePatterns: ['<rootDir>/e2e/'], | ||
setupFiles: ['<rootDir>/_jestsetup.js'], | ||
moduleNameMapper: { | ||
'\\.css$': '<rootDir>/__mocks__/styleMock.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
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.
Puppeteer headless doesn't support extensions, right now.
puppeteer/puppeteer#2819 (comment)