Skip to content

Commit

Permalink
Merge pull request #46 from tamarasaurus/setup-e2e-ci
Browse files Browse the repository at this point in the history
Run example in CI
  • Loading branch information
tamarasaurus authored Jul 22, 2024
2 parents 3e443f1 + a5859fc commit 54afb32
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 164 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ jobs:
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test
run: npm test
- name: Run example using Puppeteer
run: npm run example
137 changes: 0 additions & 137 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"watch": "tsc -w -p .",
"lint": "eslint **/*.ts",
"lint-fix": "eslint --fix **/*.ts",
"example": "ts-node ./src/examples/scrape",
"example": "ts-node ./src/examples/e2e",
"example-wait": "ts-node ./src/examples/waitForPageLoadSelector.ts"
},
"repository": {
Expand Down Expand Up @@ -66,7 +66,6 @@
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.2.1",
"globals": "^15.8.0",
"sinon": "^18.0.0",
"source-map-support": "^0.5.21",
"ts-node": "^10.9.2",
"tslint-config-airbnb": "^5.11.2",
Expand Down
11 changes: 9 additions & 2 deletions src/examples/scrape.ts → src/examples/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@ const contract = {
},
};

const scraper = new Scraper('https://webscraper.io/test-sites/e-commerce/allinone', contract);
const scraper = new Scraper(
'https://webscraper.io/test-sites/e-commerce/allinone',
contract,
);

scraper.scrapePage().then(recipes => {
console.log(recipes);
if (recipes.length === 0) {
process.exit(1);
} else {
console.log('Scraped successfully');
}
});
11 changes: 5 additions & 6 deletions test/integration/scrape.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Scraper from '../../index';
import { ScrapedPage } from '../../src/fetcher/fetcher';
import * as sinon from 'sinon';
import { expect, describe, it } from 'vitest'
import { expect, it, vi } from 'vitest';

const url = 'http://characters.com';

Expand Down Expand Up @@ -134,7 +133,7 @@ it('returns scraped data for a url and contract', () => {
};

const scraper = new Scraper(url, contract);
scraper.getFetcher = sinon.stub().returns(new FakeFetcher(url));
scraper.getFetcher = vi.fn().mockImplementation(() => new FakeFetcher(url));

const expectedData = [
{
Expand Down Expand Up @@ -227,7 +226,7 @@ it('scrapes a json schema script tag for a url and contract', () => {
},
];

scraper.getFetcher = sinon.stub().returns(new FakeFetcher(url));
scraper.getFetcher = vi.fn().mockImplementation(() => new FakeFetcher(url));

return scraper
.scrapePage()
Expand Down Expand Up @@ -256,7 +255,7 @@ it('scrapes raw html from a script tag', () => {
const scraper = new Scraper(url, contract);
const expectedData = [{ names: JSON.stringify({ first: 'Han Solo' }) }];

scraper.getFetcher = sinon.stub().returns(new FakeFetcher(url));
scraper.getFetcher = vi.fn().mockImplementation(() => new FakeFetcher(url));

return scraper
.scrapePage()
Expand Down Expand Up @@ -284,7 +283,7 @@ it('returns the scraped page object', () => {

const scraper = new Scraper(url, contract);

scraper.getFetcher = sinon.stub().returns(new FakeFetcher(url));
scraper.getFetcher = vi.fn().mockImplementation(() => new FakeFetcher(url));

return scraper
.getPageContents()
Expand Down
7 changes: 4 additions & 3 deletions test/unit/fetcher/puppeteer.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import PuppeteerFetcher from '../../../src/fetcher/puppeteer';
import * as sinon from 'sinon';
import { expect, describe, it } from 'vitest'
import { expect, describe, it, vi } from 'vitest';

class FakePage {
setUserAgent() {}
Expand Down Expand Up @@ -35,7 +34,9 @@ class FakePuppeteer {
}

const puppeteerFetcher = new PuppeteerFetcher('http://leboncoin.com');
puppeteerFetcher.getBrowserType = sinon.stub().returns(new FakePuppeteer());
puppeteerFetcher.getBrowserType = vi
.fn()
.mockImplementation(() => new FakePuppeteer());

describe('it fetches data from puppeteer', () => {
it('sets up the browser', async () => {
Expand Down
7 changes: 4 additions & 3 deletions test/unit/fetcher/request.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as sinon from 'sinon';
import RequestFetcher from '../../../src/fetcher/request';
import { expect, describe, it } from 'vitest'
import { expect, describe, it, vi } from 'vitest';

class FakeRequest {
get(options: any) {
Expand All @@ -18,7 +17,9 @@ class FakeRequest {
describe('it gets page contents using request', () => {
it('gets encoded page contents for a given url', () => {
const request = new RequestFetcher('http://whatever.com');
request.getRequestLibrary = sinon.stub().returns(new FakeRequest());
request.getRequestLibrary = vi
.fn()
.mockImplementation(() => new FakeRequest());

const expectedPage = {
encoding: 'utf-8',
Expand Down
11 changes: 5 additions & 6 deletions test/unit/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import Scraper from '../../index';
import PuppeteerFetcher from '../../src/fetcher/puppeteer';
import { ScrapedPage } from '../../src/fetcher/fetcher';
import HTMLProvider from '../../src/provider/html';
import * as sinon from 'sinon';
import RequestFetcher from '../../src/fetcher/request';
import { expect, describe, it, beforeEach } from 'vitest'
import { expect, describe, it, beforeEach, vi } from 'vitest';

const contract = {
itemSelector: 'li[itemtype=http://schema.org/Offer]',
Expand Down Expand Up @@ -115,10 +114,10 @@ describe('Scrapes a URL based on JSON configuration', () => {
}
}

scraper.getFetcher = sinon
.stub()
.returns(new FakeFetcher('http://leboncoin.com'));
scraper.getProvider = sinon.stub().returns(new ProviderStub());
scraper.getFetcher = vi
.fn()
.mockImplementation(() => new FakeFetcher('http://leboncoin.com'));
scraper.getProvider = vi.fn().mockImplementation(() => new ProviderStub());

return scraper
.scrapePage()
Expand Down
5 changes: 1 addition & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,5 @@
"resolveJsonModule": true,
"esModuleInterop": true
},
"include": [
"src/**/*.ts",
"index.ts"
]
"include": ["src/**/*.ts", "index.ts", "test/e2e/scrape.ts"]
}

0 comments on commit 54afb32

Please sign in to comment.