Skip to content

Commit

Permalink
Add a test for CommonJS caching behavior
Browse files Browse the repository at this point in the history
Summary: Adds a test demonstrating that identical `require()` calls in the same basic block can return different values in the presence of circular dependencies.

Reviewed By: avp

Differential Revision: D25223184

fbshipit-source-id: 00c823b1f03e9f6f663ba56b089dc8624b1d0ba1
  • Loading branch information
motiz88 authored and facebook-github-bot committed Nov 30, 2020
1 parent d0e0e18 commit ac75660
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
21 changes: 21 additions & 0 deletions test/hermes/cjs/cjs-caching-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

// RUN: %hermes -O0 -commonjs %S/cjs-caching-1.js %S/cjs-caching-2.js %S/cjs-caching-3.js | %FileCheck --match-full-lines %s
// RUN: %hermes -O -fstatic-require -commonjs %S/cjs-caching-1.js %S/cjs-caching-2.js %S/cjs-caching-3.js | %FileCheck --match-full-lines %s
// RUN: %hermes -O -commonjs %S/cjs-caching-1.js %S/cjs-caching-2.js %S/cjs-caching-3.js | %FileCheck --match-full-lines %s

const copy1 = require('./cjs-caching-2.js');
copy1.recreateExports();
const copy2 = require('./cjs-caching-2.js');
print('With no dependency cycle:');
print(copy1 === copy2 ? 'Copies are equal' : 'Copies are different');

// CHECK: With a dependency cycle:
// CHECK-NEXT: Copies are different
// CHECK-NEXT: With no dependency cycle:
// CHECK-NEXT: Copies are equal
16 changes: 16 additions & 0 deletions test/hermes/cjs/cjs-caching-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

// RUN: true

function recreateExports() {
module.exports = {recreateExports};
}

recreateExports();

require('./cjs-caching-3.js');
14 changes: 14 additions & 0 deletions test/hermes/cjs/cjs-caching-3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

// RUN: true

const copy1 = require('./cjs-caching-2.js');
copy1.recreateExports();
const copy2 = require('./cjs-caching-2.js');
print('With a dependency cycle:');
print(copy1 === copy2 ? 'Copies are equal' : 'Copies are different');

0 comments on commit ac75660

Please sign in to comment.